Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.ts 1.6KB

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