@unshared/client 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { Loose, UnionMerge, Override, Pretty, CollectKey, MaybeLiteral, StringSplit } from '@unshared/types';
2
- import { F as FetchMethod, R as RequestOptions } from './BZ5qrH6f.js';
2
+ import { a as FetchMethod, R as RequestOptions } from './Nc32BOzg.js';
3
3
  import { OpenAPI } from 'openapi-types';
4
4
 
5
5
  /** Get the base URL of an OpenAPI specification. */
@@ -29,7 +29,7 @@ declare namespace OpenAPIV2 {
29
29
  } ? `${Scheme}://${Host}${BasePath}` : string;
30
30
  /*************************************************************************/
31
31
  /*************************************************************************/
32
- type InferSchemaObject<T> = T extends {
32
+ type InferSchemaObject<T> = (T extends {
33
33
  properties: infer P extends Record<string, any>;
34
34
  required: Array<infer R extends string>;
35
35
  } ? ({
@@ -40,11 +40,15 @@ declare namespace OpenAPIV2 {
40
40
  properties: infer P extends Record<string, any>;
41
41
  } ? {
42
42
  [K in keyof P]?: InferSchema<P[K]>;
43
- } : T extends {
43
+ } : object) & (T extends {
44
44
  additionalProperties: infer U extends Record<string, any>;
45
- } ? Record<string, InferSchema<U>> : Record<string, unknown>;
45
+ } ? Record<string, InferSchema<U>> : T extends {
46
+ additionalProperties: true;
47
+ } ? Record<string, any> : T extends {
48
+ additionalProperties: false;
49
+ } ? object : Record<string, any>);
46
50
  type InferSchemaArray<T> = T extends {
47
- items?: infer U;
51
+ items?: infer U extends object;
48
52
  } ? Array<InferSchema<U>> : unknown[];
49
53
  type InferSchema<T> = Loose<(T extends {
50
54
  anyOf: Array<infer U>;
@@ -161,4 +161,4 @@ interface RequestOptions<Method extends FetchMethod = FetchMethod, BaseUrl exten
161
161
  */
162
162
  declare function request(route: string, options?: RequestOptions): Promise<unknown>;
163
163
 
164
- export { type FetchMethod as F, type RequestOptions as R, type SearchParamsObject as S, type ToSearchParamsOptions as T, type FetchOptions as a, type RequestContext as b, type FetchHeaders as c, type SearchArrayFormat as d, parseRequest as p, request as r, toSearchParams as t };
164
+ export { type FetchOptions as F, type RequestOptions as R, type SearchParamsObject as S, type ToSearchParamsOptions as T, type FetchMethod as a, type RequestContext as b, type FetchHeaders as c, type SearchArrayFormat as d, parseRequest as p, request as r, toSearchParams as t };
@@ -14,70 +14,15 @@ class Client {
14
14
  constructor(options = {}) {
15
15
  this.options = options;
16
16
  }
17
- /**
18
- * Fetch a route from the API and return the `Response` object. If the client was instantiated with an
19
- * application, the route name will be inferred from the application routes. Otherwise, you
20
- * can pass the route name as a string.
21
- *
22
- * @param route The name of the route to fetch.
23
- * @param options The options to pass to the request.
24
- * @returns The response from the server.
25
- */
26
17
  fetch(route, options) {
27
18
  return request.fetch(route, { ...this.options, ...options });
28
19
  }
29
- /**
30
- * Fetch a route from the API and return the data. If the client was instantiated with an
31
- * application, the route name will be inferred from the application routes. Otherwise, you
32
- * can pass the route name as a string.
33
- *
34
- * @param route The name of the route to fetch.
35
- * @param options The options to pass to the request.
36
- * @returns The data from the API.
37
- * @example
38
- * // Declare the application type.
39
- * type App = Application<[ModuleProduct]>
40
- *
41
- * // Create a type-safe client for the application.
42
- * const request = createClient<App>()
43
- *
44
- * // Fetch the data from the API.
45
- * const data = request('GET /api/product/:id', { data: { id: '1' } })
46
- */
47
20
  request(route, options) {
48
21
  return request.request(route, { ...this.options, ...options });
49
22
  }
50
- /**
51
- * Attempt to fetch a route from the API and return the data. If the client was instantiated with an
52
- * application, the route name will be inferred from the application routes. Otherwise, you
53
- * can pass the route name as a string.
54
- *
55
- * @param route The name of the route to fetch.
56
- * @param options The options to pass to the request.
57
- * @returns A result object with either the data or an error.
58
- * @example
59
- * // Declare the application type.
60
- * type App = Application<[ModuleProduct]>
61
- *
62
- * // Create a type-safe client for the application.
63
- * const request = createClient<App>()
64
- *
65
- * // Fetch the data from the API.
66
- * const { data, error } = requestAttempt('GET /api/product/:id', { data: { id: '1' } })
67
- * if (error) console.error(error)
68
- * else console.log(data)
69
- */
70
23
  requestAttempt(route, options) {
71
24
  return attempt.attempt(() => this.request(route, options));
72
25
  }
73
- /**
74
- * Create a new WebSocket connection to the server with the given path. The connection will
75
- * automatically reconnect if the connection is closed unexpectedly.
76
- *
77
- * @param channel The path to connect to.
78
- * @param options The options to pass to the connection.
79
- * @returns The WebSocket connection.
80
- */
81
26
  connect(channel, options) {
82
27
  return connect.connect(channel, { baseUrl: this.options.baseUrl, ...options });
83
28
  }
@@ -1 +1 @@
1
- {"version":3,"file":"createClient.cjs","sources":["../createClient.ts"],"sourcesContent":["import type { Awaitable } from '@unshared/functions'\nimport type { Result } from '@unshared/functions/attempt'\nimport type { ServiceOptions } from './createService'\nimport type { OpenAPILike, OpenAPIOptionsMap } from './openapi'\nimport type { RequestOptions } from './utils'\nimport type { ConnectOptions, WebSocketChannel } from './websocket'\nimport { attempt } from '@unshared/functions/attempt'\nimport { fetch } from './utils/fetch'\nimport { request } from './utils/request'\nimport { connect } from './websocket/connect'\n\ntype Data<T extends RequestOptions> = T extends RequestOptions<any, any, any, any, any, any, infer R, any> ? R : unknown\ntype Routes = Record<string, RequestOptions>\ntype Channels = Record<string, ConnectOptions>\n\nexport class Client<T extends Routes = Routes, U extends Channels = Channels> {\n\n /**\n * Create a new client for the application.\n *\n * @param options The options to pass to the client.\n * @example new Client({ baseUrl: 'https://api.example.com' })\n */\n constructor(public options: RequestOptions = {}) {}\n\n /**\n * Fetch a route from the API and return the `Response` object. If the client was instantiated with an\n * application, the route name will be inferred from the application routes. Otherwise, you\n * can pass the route name as a string.\n *\n * @param route The name of the route to fetch.\n * @param options The options to pass to the request.\n * @returns The response from the server.\n */\n public fetch<K extends keyof T & string>(route: K, options?: T[K]): Promise<Response & { json: () => Promise<Data<T[K]>> }> {\n return fetch(route, { ...this.options, ...options })\n }\n\n /**\n * Fetch a route from the API and return the data. If the client was instantiated with an\n * application, the route name will be inferred from the application routes. Otherwise, you\n * can pass the route name as a string.\n *\n * @param route The name of the route to fetch.\n * @param options The options to pass to the request.\n * @returns The data from the API.\n * @example\n * // Declare the application type.\n * type App = Application<[ModuleProduct]>\n *\n * // Create a type-safe client for the application.\n * const request = createClient<App>()\n *\n * // Fetch the data from the API.\n * const data = request('GET /api/product/:id', { data: { id: '1' } })\n */\n public request<K extends keyof T & string>(route: K, options?: T[K]): Promise<Data<T[K]>> {\n return request(route, { ...this.options, ...options }) as Promise<Data<T[K]>>\n }\n\n /**\n * Attempt to fetch a route from the API and return the data. If the client was instantiated with an\n * application, the route name will be inferred from the application routes. Otherwise, you\n * can pass the route name as a string.\n *\n * @param route The name of the route to fetch.\n * @param options The options to pass to the request.\n * @returns A result object with either the data or an error.\n * @example\n * // Declare the application type.\n * type App = Application<[ModuleProduct]>\n *\n * // Create a type-safe client for the application.\n * const request = createClient<App>()\n *\n * // Fetch the data from the API.\n * const { data, error } = requestAttempt('GET /api/product/:id', { data: { id: '1' } })\n * if (error) console.error(error)\n * else console.log(data)\n */\n public requestAttempt<K extends keyof T & string>(route: K, options?: T[K]): Promise<Result<Data<T[K]>>> {\n return attempt(() => this.request(route, options))\n }\n\n /**\n * Create a new WebSocket connection to the server with the given path. The connection will\n * automatically reconnect if the connection is closed unexpectedly.\n *\n * @param channel The path to connect to.\n * @param options The options to pass to the connection.\n * @returns The WebSocket connection.\n */\n public connect<P extends keyof U & string>(channel: P, options?: U[P]): Awaitable<WebSocketChannel<U[P]>, WebSocketChannel<U[P]>> {\n return connect(channel, { baseUrl: this.options.baseUrl, ...options }) as Awaitable<WebSocketChannel<U[P]>, WebSocketChannel<U[P]>>\n }\n}\n\n/**\n * Create a new type-safe client for the application. The client can be used to fetch data from\n * the API and connect to the server using WebSockets with the given path.\n *\n * @param options The options to pass to the client.\n * @returns The client object with the request method.\n * @example\n * // Create a type-safe client for the application.\n * const client = createClient<[ModuleUser]>()\n *\n * // Fetch the data from the API.\n * const data = await client.request('GET /api/user/:id', { id: '1' })\n *\n * // Use the data from the API.\n * console.log(data) // { id: '1', name: 'John Doe' }\n */\nexport function createClient<T extends OpenAPILike>(options?: ServiceOptions<T>): Client<OpenAPIOptionsMap<T>>\n\n/**\n * Create a new type-safe client for the application. The client can be used to fetch data from\n * the API and connect to the server using WebSockets with the given path.\n *\n * @param options The options to pass to the client.\n * @returns The client object with the request method.\n * @example\n * // Create a type-safe client for the application.\n * const client = createClient<[ModuleUser]>()\n *\n * // Fetch the data from the API.\n * const data = await client.request('GET /api/user/:id', { id: '1' })\n *\n * // Use the data from the API.\n * console.log(data) // { id: '1', name: 'John Doe' }\n */\nexport function createClient<T extends Routes = Routes, V extends Channels = Channels>(options?: RequestOptions): Client<T, V>\nexport function createClient(options?: RequestOptions): Client {\n return new Client(options)\n}\n"],"names":["fetch","request","attempt","connect"],"mappings":";;;;;;AAeO,MAAM,OAAiE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQ5E,YAAmB,UAA0B,IAAI;AAA9B,SAAA,UAAA;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWZ,MAAkC,OAAU,SAAyE;AACnH,WAAAA,QAAAA,MAAM,OAAO,EAAE,GAAG,KAAK,SAAS,GAAG,SAAS;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAqB9C,QAAoC,OAAU,SAAqC;AACjF,WAAAC,QAAAA,QAAQ,OAAO,EAAE,GAAG,KAAK,SAAS,GAAG,SAAS;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAuBhD,eAA2C,OAAU,SAA6C;AACvG,WAAOC,QAAAA,QAAQ,MAAM,KAAK,QAAQ,OAAO,OAAO,CAAC;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAW5C,QAAoC,SAAY,SAA2E;AACzH,WAAAC,QAAA,QAAQ,SAAS,EAAE,SAAS,KAAK,QAAQ,SAAS,GAAG,SAAS;AAAA,EAAA;AAEzE;AAqCO,SAAS,aAAa,SAAkC;AACtD,SAAA,IAAI,OAAO,OAAO;AAC3B;;;"}
1
+ {"version":3,"file":"createClient.cjs","sources":["../createClient.ts"],"sourcesContent":["import type { Awaitable } from '@unshared/functions'\nimport type { Result } from '@unshared/functions/attempt'\nimport type { ServiceOptions } from './createService'\nimport type { OpenAPILike, OpenAPIOptionsMap } from './openapi'\nimport type { FetchOptions, RequestOptions } from './utils'\nimport type { ConnectOptions, WebSocketChannel } from './websocket'\nimport { attempt } from '@unshared/functions/attempt'\nimport { fetch } from './utils/fetch'\nimport { request } from './utils/request'\nimport { connect } from './websocket/connect'\n\ntype Data<T extends RequestOptions> = T extends RequestOptions<any, any, any, any, any, any, infer R, any> ? R : unknown\ntype Routes = Record<string, RequestOptions>\ntype Channels = Record<string, ConnectOptions>\n\nexport class Client<T extends Routes = Routes, U extends Channels = Channels> {\n\n /**\n * Create a new client for the application.\n *\n * @param options The options to pass to the client.\n * @example new Client({ baseUrl: 'https://api.example.com' })\n */\n constructor(public options: RequestOptions = {}) {}\n\n /**\n * Fetch a route from the API and return the `Response` object. If the client was instantiated with an\n * application, the route name will be inferred from the application routes. Otherwise, you\n * can pass the route name as a string.\n *\n * @param route The name of the route to fetch.\n * @param options The options to pass to the request.\n * @returns The response from the server.\n */\n public fetch<K extends keyof T & string>(route: K, options?: T[K]): Promise<Response & { json: () => Promise<Data<T[K]>> }>\n public fetch(route: string, options?: FetchOptions): Promise<Response>\n public fetch(route: string, options?: FetchOptions): Promise<Response> {\n return fetch(route, { ...this.options, ...options })\n }\n\n /**\n * Fetch a route from the API and return the data. If the client was instantiated with an\n * application, the route name will be inferred from the application routes. Otherwise, you\n * can pass the route name as a string.\n *\n * @param route The name of the route to fetch.\n * @param options The options to pass to the request.\n * @returns The data from the API.\n * @example\n * // Declare the application type.\n * type App = Application<[ModuleProduct]>\n *\n * // Create a type-safe client for the application.\n * const request = createClient<App>()\n *\n * // Fetch the data from the API.\n * const data = request('GET /api/product/:id', { data: { id: '1' } })\n */\n public request<K extends keyof T & string>(route: K, options?: T[K]): Promise<Data<T[K]>>\n public request(route: string, options?: RequestOptions): Promise<unknown>\n public request(route: string, options?: RequestOptions): Promise<unknown> {\n return request(route, { ...this.options, ...options })\n }\n\n /**\n * Attempt to fetch a route from the API and return the data. If the client was instantiated with an\n * application, the route name will be inferred from the application routes. Otherwise, you\n * can pass the route name as a string.\n *\n * @param route The name of the route to fetch.\n * @param options The options to pass to the request.\n * @returns A result object with either the data or an error.\n * @example\n * // Declare the application type.\n * type App = Application<[ModuleProduct]>\n *\n * // Create a type-safe client for the application.\n * const request = createClient<App>()\n *\n * // Fetch the data from the API.\n * const { data, error } = requestAttempt('GET /api/product/:id', { data: { id: '1' } })\n * if (error) console.error(error)\n * else console.log(data)\n */\n public requestAttempt<K extends keyof T & string>(route: K, options?: T[K]): Promise<Result<Data<T[K]>>>\n public requestAttempt(route: string, options?: RequestOptions): Promise<Result<unknown>>\n public requestAttempt(route: string, options?: RequestOptions): Promise<Result<unknown>> {\n return attempt(() => this.request(route, options))\n }\n\n /**\n * Create a new WebSocket connection to the server with the given path. The connection will\n * automatically reconnect if the connection is closed unexpectedly.\n *\n * @param channel The path to connect to.\n * @param options The options to pass to the connection.\n * @returns The WebSocket connection.\n */\n public connect<P extends keyof U & string>(channel: P, options?: U[P]): Awaitable<WebSocketChannel<U[P]>, WebSocketChannel<U[P]>>\n public connect(channel: string, options?: ConnectOptions): Awaitable<WebSocketChannel, WebSocketChannel>\n public connect(channel: string, options?: ConnectOptions): Awaitable<WebSocketChannel, WebSocketChannel> {\n return connect(channel, { baseUrl: this.options.baseUrl, ...options })\n }\n}\n\n/**\n * Create a new type-safe client for the application. The client can be used to fetch data from\n * the API and connect to the server using WebSockets with the given path.\n *\n * @param options The options to pass to the client.\n * @returns The client object with the request method.\n * @example\n * // Create a type-safe client for the application.\n * const client = createClient<[ModuleUser]>()\n *\n * // Fetch the data from the API.\n * const data = await client.request('GET /api/user/:id', { id: '1' })\n *\n * // Use the data from the API.\n * console.log(data) // { id: '1', name: 'John Doe' }\n */\nexport function createClient<T extends OpenAPILike>(options?: ServiceOptions<T>): Client<OpenAPIOptionsMap<T>>\n\n/**\n * Create a new type-safe client for the application. The client can be used to fetch data from\n * the API and connect to the server using WebSockets with the given path.\n *\n * @param options The options to pass to the client.\n * @returns The client object with the request method.\n * @example\n * // Create a type-safe client for the application.\n * const client = createClient<[ModuleUser]>()\n *\n * // Fetch the data from the API.\n * const data = await client.request('GET /api/user/:id', { id: '1' })\n *\n * // Use the data from the API.\n * console.log(data) // { id: '1', name: 'John Doe' }\n */\nexport function createClient<T extends Routes = Routes, V extends Channels = Channels>(options?: RequestOptions): Client<T, V>\nexport function createClient(options?: RequestOptions): Client {\n return new Client(options)\n}\n"],"names":["fetch","request","attempt","connect"],"mappings":";;;;;;AAeO,MAAM,OAAiE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQ5E,YAAmB,UAA0B,IAAI;AAA9B,SAAA,UAAA;AAAA,EAAA;AAAA,EAaZ,MAAM,OAAe,SAA2C;AAC9D,WAAAA,QAAAA,MAAM,OAAO,EAAE,GAAG,KAAK,SAAS,GAAG,SAAS;AAAA,EAAA;AAAA,EAuB9C,QAAQ,OAAe,SAA4C;AACjE,WAAAC,QAAAA,QAAQ,OAAO,EAAE,GAAG,KAAK,SAAS,GAAG,SAAS;AAAA,EAAA;AAAA,EAyBhD,eAAe,OAAe,SAAoD;AACvF,WAAOC,QAAAA,QAAQ,MAAM,KAAK,QAAQ,OAAO,OAAO,CAAC;AAAA,EAAA;AAAA,EAa5C,QAAQ,SAAiB,SAAyE;AAChG,WAAAC,QAAA,QAAQ,SAAS,EAAE,SAAS,KAAK,QAAQ,SAAS,GAAG,SAAS;AAAA,EAAA;AAEzE;AAqCO,SAAS,aAAa,SAAkC;AACtD,SAAA,IAAI,OAAO,OAAO;AAC3B;;;"}
@@ -1,13 +1,13 @@
1
1
  import { Awaitable } from '@unshared/functions';
2
2
  import { Result } from '@unshared/functions/attempt';
3
3
  import { ServiceOptions } from './createService.js';
4
- import { O as OpenAPILike, a as OpenAPIOptionsMap } from './chunks/Bs2VarsP.js';
5
- import { R as RequestOptions } from './chunks/BZ5qrH6f.js';
4
+ import { R as RequestOptions, F as FetchOptions } from './chunks/Nc32BOzg.js';
5
+ import { O as OpenAPILike, a as OpenAPIOptionsMap } from './chunks/5p4H9ZaI.js';
6
6
  import { WebSocketChannel, ConnectOptions } from './websocket.js';
7
7
  import '@unshared/types';
8
- import 'openapi-types';
9
8
  import './HttpHeaders.js';
10
9
  import './HttpMethods.js';
10
+ import 'openapi-types';
11
11
 
12
12
  type Data<T extends RequestOptions> = T extends RequestOptions<any, any, any, any, any, any, infer R, any> ? R : unknown;
13
13
  type Routes = Record<string, RequestOptions>;
@@ -33,6 +33,7 @@ declare class Client<T extends Routes = Routes, U extends Channels = Channels> {
33
33
  fetch<K extends keyof T & string>(route: K, options?: T[K]): Promise<Response & {
34
34
  json: () => Promise<Data<T[K]>>;
35
35
  }>;
36
+ fetch(route: string, options?: FetchOptions): Promise<Response>;
36
37
  /**
37
38
  * Fetch a route from the API and return the data. If the client was instantiated with an
38
39
  * application, the route name will be inferred from the application routes. Otherwise, you
@@ -52,6 +53,7 @@ declare class Client<T extends Routes = Routes, U extends Channels = Channels> {
52
53
  * const data = request('GET /api/product/:id', { data: { id: '1' } })
53
54
  */
54
55
  request<K extends keyof T & string>(route: K, options?: T[K]): Promise<Data<T[K]>>;
56
+ request(route: string, options?: RequestOptions): Promise<unknown>;
55
57
  /**
56
58
  * Attempt to fetch a route from the API and return the data. If the client was instantiated with an
57
59
  * application, the route name will be inferred from the application routes. Otherwise, you
@@ -73,6 +75,7 @@ declare class Client<T extends Routes = Routes, U extends Channels = Channels> {
73
75
  * else console.log(data)
74
76
  */
75
77
  requestAttempt<K extends keyof T & string>(route: K, options?: T[K]): Promise<Result<Data<T[K]>>>;
78
+ requestAttempt(route: string, options?: RequestOptions): Promise<Result<unknown>>;
76
79
  /**
77
80
  * Create a new WebSocket connection to the server with the given path. The connection will
78
81
  * automatically reconnect if the connection is closed unexpectedly.
@@ -82,6 +85,7 @@ declare class Client<T extends Routes = Routes, U extends Channels = Channels> {
82
85
  * @returns The WebSocket connection.
83
86
  */
84
87
  connect<P extends keyof U & string>(channel: P, options?: U[P]): Awaitable<WebSocketChannel<U[P]>, WebSocketChannel<U[P]>>;
88
+ connect(channel: string, options?: ConnectOptions): Awaitable<WebSocketChannel, WebSocketChannel>;
85
89
  }
86
90
  /**
87
91
  * Create a new type-safe client for the application. The client can be used to fetch data from
@@ -15,70 +15,15 @@ class Client {
15
15
  constructor(options = {}) {
16
16
  this.options = options;
17
17
  }
18
- /**
19
- * Fetch a route from the API and return the `Response` object. If the client was instantiated with an
20
- * application, the route name will be inferred from the application routes. Otherwise, you
21
- * can pass the route name as a string.
22
- *
23
- * @param route The name of the route to fetch.
24
- * @param options The options to pass to the request.
25
- * @returns The response from the server.
26
- */
27
18
  fetch(route, options) {
28
19
  return fetch(route, { ...this.options, ...options });
29
20
  }
30
- /**
31
- * Fetch a route from the API and return the data. If the client was instantiated with an
32
- * application, the route name will be inferred from the application routes. Otherwise, you
33
- * can pass the route name as a string.
34
- *
35
- * @param route The name of the route to fetch.
36
- * @param options The options to pass to the request.
37
- * @returns The data from the API.
38
- * @example
39
- * // Declare the application type.
40
- * type App = Application<[ModuleProduct]>
41
- *
42
- * // Create a type-safe client for the application.
43
- * const request = createClient<App>()
44
- *
45
- * // Fetch the data from the API.
46
- * const data = request('GET /api/product/:id', { data: { id: '1' } })
47
- */
48
21
  request(route, options) {
49
22
  return request(route, { ...this.options, ...options });
50
23
  }
51
- /**
52
- * Attempt to fetch a route from the API and return the data. If the client was instantiated with an
53
- * application, the route name will be inferred from the application routes. Otherwise, you
54
- * can pass the route name as a string.
55
- *
56
- * @param route The name of the route to fetch.
57
- * @param options The options to pass to the request.
58
- * @returns A result object with either the data or an error.
59
- * @example
60
- * // Declare the application type.
61
- * type App = Application<[ModuleProduct]>
62
- *
63
- * // Create a type-safe client for the application.
64
- * const request = createClient<App>()
65
- *
66
- * // Fetch the data from the API.
67
- * const { data, error } = requestAttempt('GET /api/product/:id', { data: { id: '1' } })
68
- * if (error) console.error(error)
69
- * else console.log(data)
70
- */
71
24
  requestAttempt(route, options) {
72
25
  return attempt(() => this.request(route, options));
73
26
  }
74
- /**
75
- * Create a new WebSocket connection to the server with the given path. The connection will
76
- * automatically reconnect if the connection is closed unexpectedly.
77
- *
78
- * @param channel The path to connect to.
79
- * @param options The options to pass to the connection.
80
- * @returns The WebSocket connection.
81
- */
82
27
  connect(channel, options) {
83
28
  return connect(channel, { baseUrl: this.options.baseUrl, ...options });
84
29
  }
@@ -1 +1 @@
1
- {"version":3,"file":"createClient.js","sources":["../createClient.ts"],"sourcesContent":["import type { Awaitable } from '@unshared/functions'\nimport type { Result } from '@unshared/functions/attempt'\nimport type { ServiceOptions } from './createService'\nimport type { OpenAPILike, OpenAPIOptionsMap } from './openapi'\nimport type { RequestOptions } from './utils'\nimport type { ConnectOptions, WebSocketChannel } from './websocket'\nimport { attempt } from '@unshared/functions/attempt'\nimport { fetch } from './utils/fetch'\nimport { request } from './utils/request'\nimport { connect } from './websocket/connect'\n\ntype Data<T extends RequestOptions> = T extends RequestOptions<any, any, any, any, any, any, infer R, any> ? R : unknown\ntype Routes = Record<string, RequestOptions>\ntype Channels = Record<string, ConnectOptions>\n\nexport class Client<T extends Routes = Routes, U extends Channels = Channels> {\n\n /**\n * Create a new client for the application.\n *\n * @param options The options to pass to the client.\n * @example new Client({ baseUrl: 'https://api.example.com' })\n */\n constructor(public options: RequestOptions = {}) {}\n\n /**\n * Fetch a route from the API and return the `Response` object. If the client was instantiated with an\n * application, the route name will be inferred from the application routes. Otherwise, you\n * can pass the route name as a string.\n *\n * @param route The name of the route to fetch.\n * @param options The options to pass to the request.\n * @returns The response from the server.\n */\n public fetch<K extends keyof T & string>(route: K, options?: T[K]): Promise<Response & { json: () => Promise<Data<T[K]>> }> {\n return fetch(route, { ...this.options, ...options })\n }\n\n /**\n * Fetch a route from the API and return the data. If the client was instantiated with an\n * application, the route name will be inferred from the application routes. Otherwise, you\n * can pass the route name as a string.\n *\n * @param route The name of the route to fetch.\n * @param options The options to pass to the request.\n * @returns The data from the API.\n * @example\n * // Declare the application type.\n * type App = Application<[ModuleProduct]>\n *\n * // Create a type-safe client for the application.\n * const request = createClient<App>()\n *\n * // Fetch the data from the API.\n * const data = request('GET /api/product/:id', { data: { id: '1' } })\n */\n public request<K extends keyof T & string>(route: K, options?: T[K]): Promise<Data<T[K]>> {\n return request(route, { ...this.options, ...options }) as Promise<Data<T[K]>>\n }\n\n /**\n * Attempt to fetch a route from the API and return the data. If the client was instantiated with an\n * application, the route name will be inferred from the application routes. Otherwise, you\n * can pass the route name as a string.\n *\n * @param route The name of the route to fetch.\n * @param options The options to pass to the request.\n * @returns A result object with either the data or an error.\n * @example\n * // Declare the application type.\n * type App = Application<[ModuleProduct]>\n *\n * // Create a type-safe client for the application.\n * const request = createClient<App>()\n *\n * // Fetch the data from the API.\n * const { data, error } = requestAttempt('GET /api/product/:id', { data: { id: '1' } })\n * if (error) console.error(error)\n * else console.log(data)\n */\n public requestAttempt<K extends keyof T & string>(route: K, options?: T[K]): Promise<Result<Data<T[K]>>> {\n return attempt(() => this.request(route, options))\n }\n\n /**\n * Create a new WebSocket connection to the server with the given path. The connection will\n * automatically reconnect if the connection is closed unexpectedly.\n *\n * @param channel The path to connect to.\n * @param options The options to pass to the connection.\n * @returns The WebSocket connection.\n */\n public connect<P extends keyof U & string>(channel: P, options?: U[P]): Awaitable<WebSocketChannel<U[P]>, WebSocketChannel<U[P]>> {\n return connect(channel, { baseUrl: this.options.baseUrl, ...options }) as Awaitable<WebSocketChannel<U[P]>, WebSocketChannel<U[P]>>\n }\n}\n\n/**\n * Create a new type-safe client for the application. The client can be used to fetch data from\n * the API and connect to the server using WebSockets with the given path.\n *\n * @param options The options to pass to the client.\n * @returns The client object with the request method.\n * @example\n * // Create a type-safe client for the application.\n * const client = createClient<[ModuleUser]>()\n *\n * // Fetch the data from the API.\n * const data = await client.request('GET /api/user/:id', { id: '1' })\n *\n * // Use the data from the API.\n * console.log(data) // { id: '1', name: 'John Doe' }\n */\nexport function createClient<T extends OpenAPILike>(options?: ServiceOptions<T>): Client<OpenAPIOptionsMap<T>>\n\n/**\n * Create a new type-safe client for the application. The client can be used to fetch data from\n * the API and connect to the server using WebSockets with the given path.\n *\n * @param options The options to pass to the client.\n * @returns The client object with the request method.\n * @example\n * // Create a type-safe client for the application.\n * const client = createClient<[ModuleUser]>()\n *\n * // Fetch the data from the API.\n * const data = await client.request('GET /api/user/:id', { id: '1' })\n *\n * // Use the data from the API.\n * console.log(data) // { id: '1', name: 'John Doe' }\n */\nexport function createClient<T extends Routes = Routes, V extends Channels = Channels>(options?: RequestOptions): Client<T, V>\nexport function createClient(options?: RequestOptions): Client {\n return new Client(options)\n}\n"],"names":[],"mappings":";;;;;;;AAeO,MAAM,OAAiE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQ5E,YAAmB,UAA0B,IAAI;AAA9B,SAAA,UAAA;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWZ,MAAkC,OAAU,SAAyE;AACnH,WAAA,MAAM,OAAO,EAAE,GAAG,KAAK,SAAS,GAAG,SAAS;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAqB9C,QAAoC,OAAU,SAAqC;AACjF,WAAA,QAAQ,OAAO,EAAE,GAAG,KAAK,SAAS,GAAG,SAAS;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAuBhD,eAA2C,OAAU,SAA6C;AACvG,WAAO,QAAQ,MAAM,KAAK,QAAQ,OAAO,OAAO,CAAC;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAW5C,QAAoC,SAAY,SAA2E;AACzH,WAAA,QAAQ,SAAS,EAAE,SAAS,KAAK,QAAQ,SAAS,GAAG,SAAS;AAAA,EAAA;AAEzE;AAqCO,SAAS,aAAa,SAAkC;AACtD,SAAA,IAAI,OAAO,OAAO;AAC3B;"}
1
+ {"version":3,"file":"createClient.js","sources":["../createClient.ts"],"sourcesContent":["import type { Awaitable } from '@unshared/functions'\nimport type { Result } from '@unshared/functions/attempt'\nimport type { ServiceOptions } from './createService'\nimport type { OpenAPILike, OpenAPIOptionsMap } from './openapi'\nimport type { FetchOptions, RequestOptions } from './utils'\nimport type { ConnectOptions, WebSocketChannel } from './websocket'\nimport { attempt } from '@unshared/functions/attempt'\nimport { fetch } from './utils/fetch'\nimport { request } from './utils/request'\nimport { connect } from './websocket/connect'\n\ntype Data<T extends RequestOptions> = T extends RequestOptions<any, any, any, any, any, any, infer R, any> ? R : unknown\ntype Routes = Record<string, RequestOptions>\ntype Channels = Record<string, ConnectOptions>\n\nexport class Client<T extends Routes = Routes, U extends Channels = Channels> {\n\n /**\n * Create a new client for the application.\n *\n * @param options The options to pass to the client.\n * @example new Client({ baseUrl: 'https://api.example.com' })\n */\n constructor(public options: RequestOptions = {}) {}\n\n /**\n * Fetch a route from the API and return the `Response` object. If the client was instantiated with an\n * application, the route name will be inferred from the application routes. Otherwise, you\n * can pass the route name as a string.\n *\n * @param route The name of the route to fetch.\n * @param options The options to pass to the request.\n * @returns The response from the server.\n */\n public fetch<K extends keyof T & string>(route: K, options?: T[K]): Promise<Response & { json: () => Promise<Data<T[K]>> }>\n public fetch(route: string, options?: FetchOptions): Promise<Response>\n public fetch(route: string, options?: FetchOptions): Promise<Response> {\n return fetch(route, { ...this.options, ...options })\n }\n\n /**\n * Fetch a route from the API and return the data. If the client was instantiated with an\n * application, the route name will be inferred from the application routes. Otherwise, you\n * can pass the route name as a string.\n *\n * @param route The name of the route to fetch.\n * @param options The options to pass to the request.\n * @returns The data from the API.\n * @example\n * // Declare the application type.\n * type App = Application<[ModuleProduct]>\n *\n * // Create a type-safe client for the application.\n * const request = createClient<App>()\n *\n * // Fetch the data from the API.\n * const data = request('GET /api/product/:id', { data: { id: '1' } })\n */\n public request<K extends keyof T & string>(route: K, options?: T[K]): Promise<Data<T[K]>>\n public request(route: string, options?: RequestOptions): Promise<unknown>\n public request(route: string, options?: RequestOptions): Promise<unknown> {\n return request(route, { ...this.options, ...options })\n }\n\n /**\n * Attempt to fetch a route from the API and return the data. If the client was instantiated with an\n * application, the route name will be inferred from the application routes. Otherwise, you\n * can pass the route name as a string.\n *\n * @param route The name of the route to fetch.\n * @param options The options to pass to the request.\n * @returns A result object with either the data or an error.\n * @example\n * // Declare the application type.\n * type App = Application<[ModuleProduct]>\n *\n * // Create a type-safe client for the application.\n * const request = createClient<App>()\n *\n * // Fetch the data from the API.\n * const { data, error } = requestAttempt('GET /api/product/:id', { data: { id: '1' } })\n * if (error) console.error(error)\n * else console.log(data)\n */\n public requestAttempt<K extends keyof T & string>(route: K, options?: T[K]): Promise<Result<Data<T[K]>>>\n public requestAttempt(route: string, options?: RequestOptions): Promise<Result<unknown>>\n public requestAttempt(route: string, options?: RequestOptions): Promise<Result<unknown>> {\n return attempt(() => this.request(route, options))\n }\n\n /**\n * Create a new WebSocket connection to the server with the given path. The connection will\n * automatically reconnect if the connection is closed unexpectedly.\n *\n * @param channel The path to connect to.\n * @param options The options to pass to the connection.\n * @returns The WebSocket connection.\n */\n public connect<P extends keyof U & string>(channel: P, options?: U[P]): Awaitable<WebSocketChannel<U[P]>, WebSocketChannel<U[P]>>\n public connect(channel: string, options?: ConnectOptions): Awaitable<WebSocketChannel, WebSocketChannel>\n public connect(channel: string, options?: ConnectOptions): Awaitable<WebSocketChannel, WebSocketChannel> {\n return connect(channel, { baseUrl: this.options.baseUrl, ...options })\n }\n}\n\n/**\n * Create a new type-safe client for the application. The client can be used to fetch data from\n * the API and connect to the server using WebSockets with the given path.\n *\n * @param options The options to pass to the client.\n * @returns The client object with the request method.\n * @example\n * // Create a type-safe client for the application.\n * const client = createClient<[ModuleUser]>()\n *\n * // Fetch the data from the API.\n * const data = await client.request('GET /api/user/:id', { id: '1' })\n *\n * // Use the data from the API.\n * console.log(data) // { id: '1', name: 'John Doe' }\n */\nexport function createClient<T extends OpenAPILike>(options?: ServiceOptions<T>): Client<OpenAPIOptionsMap<T>>\n\n/**\n * Create a new type-safe client for the application. The client can be used to fetch data from\n * the API and connect to the server using WebSockets with the given path.\n *\n * @param options The options to pass to the client.\n * @returns The client object with the request method.\n * @example\n * // Create a type-safe client for the application.\n * const client = createClient<[ModuleUser]>()\n *\n * // Fetch the data from the API.\n * const data = await client.request('GET /api/user/:id', { id: '1' })\n *\n * // Use the data from the API.\n * console.log(data) // { id: '1', name: 'John Doe' }\n */\nexport function createClient<T extends Routes = Routes, V extends Channels = Channels>(options?: RequestOptions): Client<T, V>\nexport function createClient(options?: RequestOptions): Client {\n return new Client(options)\n}\n"],"names":[],"mappings":";;;;;;;AAeO,MAAM,OAAiE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQ5E,YAAmB,UAA0B,IAAI;AAA9B,SAAA,UAAA;AAAA,EAAA;AAAA,EAaZ,MAAM,OAAe,SAA2C;AAC9D,WAAA,MAAM,OAAO,EAAE,GAAG,KAAK,SAAS,GAAG,SAAS;AAAA,EAAA;AAAA,EAuB9C,QAAQ,OAAe,SAA4C;AACjE,WAAA,QAAQ,OAAO,EAAE,GAAG,KAAK,SAAS,GAAG,SAAS;AAAA,EAAA;AAAA,EAyBhD,eAAe,OAAe,SAAoD;AACvF,WAAO,QAAQ,MAAM,KAAK,QAAQ,OAAO,OAAO,CAAC;AAAA,EAAA;AAAA,EAa5C,QAAQ,SAAiB,SAAyE;AAChG,WAAA,QAAQ,SAAS,EAAE,SAAS,KAAK,QAAQ,SAAS,GAAG,SAAS;AAAA,EAAA;AAEzE;AAqCO,SAAS,aAAa,SAAkC;AACtD,SAAA,IAAI,OAAO,OAAO;AAC3B;"}
@@ -1,6 +1,6 @@
1
1
  import { MaybeLiteral } from '@unshared/types';
2
- import { b as OperationId, c as OperationById, d as OperationOptions, e as OperationResult, S as ServerUrl, f as OpenAPIV3 } from './chunks/Bs2VarsP.js';
3
- import { R as RequestOptions } from './chunks/BZ5qrH6f.js';
2
+ import { b as OperationId, c as OperationById, d as OperationOptions, e as OperationResult, S as ServerUrl, f as OpenAPIV3 } from './chunks/5p4H9ZaI.js';
3
+ import { R as RequestOptions } from './chunks/Nc32BOzg.js';
4
4
  import 'openapi-types';
5
5
  import './HttpHeaders.js';
6
6
  import './HttpMethods.js';
package/dist/index.d.ts CHANGED
@@ -5,8 +5,8 @@ export { HttpMethod } from './HttpMethods.js';
5
5
  export { HttpStatusCode } from './HttpStatusCodes.js';
6
6
  import '@unshared/functions';
7
7
  import '@unshared/functions/attempt';
8
- import './chunks/Bs2VarsP.js';
8
+ import './chunks/Nc32BOzg.js';
9
9
  import '@unshared/types';
10
- import './chunks/BZ5qrH6f.js';
10
+ import './chunks/5p4H9ZaI.js';
11
11
  import 'openapi-types';
12
12
  import './websocket.js';
package/dist/openapi.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- export { O as OpenAPILike, a as OpenAPIOptionsMap, h as OpenAPIV2, j as OpenAPIV2Like, f as OpenAPIV3, k as OpenAPIV3Like, i as Operation, c as OperationById, n as OperationByRoute, b as OperationId, d as OperationOptions, l as OperationResponse, e as OperationResult, m as OperationRoute, S as ServerUrl, g as getServerUrl, r as resolveOperation } from './chunks/Bs2VarsP.js';
1
+ export { O as OpenAPILike, a as OpenAPIOptionsMap, h as OpenAPIV2, j as OpenAPIV2Like, f as OpenAPIV3, k as OpenAPIV3Like, i as Operation, c as OperationById, n as OperationByRoute, b as OperationId, d as OperationOptions, l as OperationResponse, e as OperationResult, m as OperationRoute, S as ServerUrl, g as getServerUrl, r as resolveOperation } from './chunks/5p4H9ZaI.js';
2
2
  import { OpenAPIV2, OpenAPIV3, OpenAPIV3_1, OpenAPI } from 'openapi-types';
3
3
  import { StringReplace, WriteableDeep, StringJoin, Substract } from '@unshared/types';
4
- import './chunks/BZ5qrH6f.js';
4
+ import './chunks/Nc32BOzg.js';
5
5
  import './HttpHeaders.js';
6
6
  import './HttpMethods.js';
7
7
 
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { a as FetchOptions, R as RequestOptions, b as RequestContext } from './chunks/BZ5qrH6f.js';
2
- export { c as FetchHeaders, F as FetchMethod, d as SearchArrayFormat, S as SearchParamsObject, T as ToSearchParamsOptions, p as parseRequest, r as request, t as toSearchParams } from './chunks/BZ5qrH6f.js';
1
+ import { F as FetchOptions, R as RequestOptions, b as RequestContext } from './chunks/Nc32BOzg.js';
2
+ export { c as FetchHeaders, a as FetchMethod, d as SearchArrayFormat, S as SearchParamsObject, T as ToSearchParamsOptions, p as parseRequest, r as request, t as toSearchParams } from './chunks/Nc32BOzg.js';
3
3
  import { Awaitable } from '@unshared/functions/awaitable';
4
4
  import { ObjectLike } from '@unshared/types';
5
5
  import './HttpHeaders.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unshared/client",
3
3
  "type": "module",
4
- "version": "0.3.3",
4
+ "version": "0.4.0",
5
5
  "license": "MIT",
6
6
  "sideEffects": false,
7
7
  "author": "Stanley Horwood <stanley@hsjm.io>",
@@ -68,12 +68,12 @@
68
68
  "LICENSE.md"
69
69
  ],
70
70
  "dependencies": {
71
- "@unshared/functions": "0.3.3",
72
- "@unshared/types": "0.3.3",
71
+ "@unshared/functions": "0.4.0",
72
+ "@unshared/types": "0.4.0",
73
73
  "openapi-types": "12.1.3"
74
74
  },
75
75
  "devDependencies": {
76
- "@unshared/scripts": "0.3.3"
76
+ "@unshared/scripts": "0.4.0"
77
77
  },
78
78
  "scripts": {
79
79
  "build:httpMethods": "tsx ./scripts/buildHttpMethods.ts",