You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.ts 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import PortalClient, { Client, ClientFn, Config as ClientConfig } from './client'
  2. import methodGenerator, { MethodFactory } from './method'
  3. import resrouceGenerator, { ResourceFactory } from './resource'
  4. // let got: ClientFn
  5. // try {
  6. // got = require('got')
  7. // } catch (_) {}
  8. import got from 'got'
  9. // function forNode (clientType: ClientType | undefined): clientType is NodeClient {
  10. // return typeof clientType === 'undefined' || (clientType.type === 'node' || clientType.type === 'got')
  11. // }
  12. // function getClient (config: PortalConfig): ClientFn {
  13. // if (forNode(config.client)) return got
  14. // throw new Error(`Unspported client config ${config.client}`)
  15. // }
  16. // export interface ClientType {
  17. // type: 'node' | 'got', // | 'browser' | 'ky' | 'custom',
  18. // }
  19. // export interface NodeClient extends ClientType {
  20. // type: 'node' | 'got',
  21. // }
  22. export interface PortalConfig {
  23. // client?: NodeClient,
  24. }
  25. export type Config = ClientConfig & PortalConfig
  26. export const clientSymbol = Symbol.for('portal:symbols:client')
  27. export interface Portal {
  28. route: MethodFactory,
  29. resource: ResourceFactory,
  30. [clientSymbol]: Client,
  31. }
  32. export function createPortalClient (config: Config): Portal {
  33. const clientFn = got // getClient(config)
  34. const client = new PortalClient(clientFn, config)
  35. const portal: Portal = {
  36. route: methodGenerator(client),
  37. resource: resrouceGenerator(client),
  38. [clientSymbol]: client,
  39. }
  40. return portal
  41. }
  42. export { MethodSpec } from './method'
  43. export { Client, rawResponseSymbol } from './client'
  44. // Can't a the moment run export { * as Joi } from 'joi'
  45. // so this clumsy way is the solution re-export
  46. import * as Joi from 'joi'
  47. export { Joi }
  48. export default createPortalClient