@unshared/client 0.3.0 → 0.3.2

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.
Files changed (48) hide show
  1. package/dist/chunks/0ZzUT3m_.js +46 -0
  2. package/dist/chunks/0ZzUT3m_.js.map +1 -0
  3. package/dist/chunks/6IxvRpmY.js +86 -0
  4. package/dist/chunks/6IxvRpmY.js.map +1 -0
  5. package/dist/chunks/BMbsYNBd.cjs +84 -0
  6. package/dist/chunks/BMbsYNBd.cjs.map +1 -0
  7. package/dist/chunks/{D51s1VII.js → BUeqbyph.js} +3 -45
  8. package/dist/chunks/BUeqbyph.js.map +1 -0
  9. package/dist/chunks/Biic1J5b.js.map +1 -1
  10. package/dist/chunks/{B92aAMq0.d.ts → CO11DuYE.d.ts} +1 -1
  11. package/dist/chunks/CYmaYL5B.cjs +45 -0
  12. package/dist/chunks/CYmaYL5B.cjs.map +1 -0
  13. package/dist/chunks/{CfKxYeRr.cjs → Cayg8606.cjs} +4 -47
  14. package/dist/chunks/Cayg8606.cjs.map +1 -0
  15. package/dist/chunks/CtW2aMuA.cjs.map +1 -1
  16. package/dist/chunks/{lMH6B5BV.js → DJJsADWD.js} +2 -2
  17. package/dist/chunks/{lMH6B5BV.js.map → DJJsADWD.js.map} +1 -1
  18. package/dist/chunks/{iA98-4f5.cjs → Du_W5H6e.cjs} +2 -2
  19. package/dist/chunks/{iA98-4f5.cjs.map → Du_W5H6e.cjs.map} +1 -1
  20. package/dist/chunks/{CjU0376e.d.ts → mHfUAX_g.d.ts} +18 -23
  21. package/dist/createClient.cjs +17 -15
  22. package/dist/createClient.cjs.map +1 -1
  23. package/dist/createClient.d.ts +44 -27
  24. package/dist/createClient.js +19 -16
  25. package/dist/createClient.js.map +1 -1
  26. package/dist/createService.cjs +2 -1
  27. package/dist/createService.cjs.map +1 -1
  28. package/dist/createService.d.ts +2 -2
  29. package/dist/createService.js +2 -1
  30. package/dist/createService.js.map +1 -1
  31. package/dist/index.cjs +7 -22
  32. package/dist/index.cjs.map +1 -1
  33. package/dist/index.d.ts +5 -6
  34. package/dist/index.js +7 -25
  35. package/dist/index.js.map +1 -1
  36. package/dist/openapi.d.ts +2 -2
  37. package/dist/utils.cjs +4 -4
  38. package/dist/utils.d.ts +2 -2
  39. package/dist/utils.js +7 -6
  40. package/dist/utils.js.map +1 -1
  41. package/dist/websocket.cjs +8 -0
  42. package/dist/websocket.cjs.map +1 -0
  43. package/dist/websocket.d.ts +143 -0
  44. package/dist/websocket.js +9 -0
  45. package/dist/websocket.js.map +1 -0
  46. package/package.json +9 -4
  47. package/dist/chunks/CfKxYeRr.cjs.map +0 -1
  48. package/dist/chunks/D51s1VII.js.map +0 -1
@@ -1,16 +1,19 @@
1
1
  import { attempt } from "@unshared/functions/attempt";
2
- import { f as fetch } from "./chunks/D51s1VII.js";
3
- import { r as request } from "./chunks/lMH6B5BV.js";
2
+ import { f as fetch } from "./chunks/BUeqbyph.js";
3
+ import { r as request } from "./chunks/DJJsADWD.js";
4
+ import { c as connect } from "./chunks/6IxvRpmY.js";
5
+ import "./chunks/0ZzUT3m_.js";
4
6
  import "@unshared/functions/awaitable";
7
+ import "@unshared/functions";
5
8
  class Client {
6
9
  /**
7
10
  * Create a new client for the application.
8
11
  *
9
- * @param initialOptions The options to pass to the client.
12
+ * @param options The options to pass to the client.
10
13
  * @example new Client({ baseUrl: 'https://api.example.com' })
11
14
  */
12
- constructor(initialOptions = {}) {
13
- this.initialOptions = initialOptions;
15
+ constructor(options = {}) {
16
+ this.options = options;
14
17
  }
15
18
  /**
16
19
  * Fetch a route from the API and return the `Response` object. If the client was instantiated with an
@@ -21,8 +24,8 @@ class Client {
21
24
  * @param options The options to pass to the request.
22
25
  * @returns The response from the server.
23
26
  */
24
- async fetch(route, options) {
25
- return await fetch(route, { ...this.initialOptions, ...options });
27
+ fetch(route, options) {
28
+ return fetch(route, { ...this.options, ...options });
26
29
  }
27
30
  /**
28
31
  * Fetch a route from the API and return the data. If the client was instantiated with an
@@ -42,15 +45,15 @@ class Client {
42
45
  * // Fetch the data from the API.
43
46
  * const data = request('GET /api/product/:id', { data: { id: '1' } })
44
47
  */
45
- async request(route, options) {
46
- return await request(route, { ...this.initialOptions, ...options });
48
+ request(route, options) {
49
+ return request(route, { ...this.options, ...options });
47
50
  }
48
51
  /**
49
52
  * Attempt to fetch a route from the API and return the data. If the client was instantiated with an
50
53
  * application, the route name will be inferred from the application routes. Otherwise, you
51
54
  * can pass the route name as a string.
52
55
  *
53
- * @param name The name of the route to fetch.
56
+ * @param route The name of the route to fetch.
54
57
  * @param options The options to pass to the request.
55
58
  * @returns A result object with either the data or an error.
56
59
  * @example
@@ -65,20 +68,20 @@ class Client {
65
68
  * if (error) console.error(error)
66
69
  * else console.log(data)
67
70
  */
68
- async requestAttempt(name, options) {
69
- return await attempt(() => this.request(name, options));
71
+ requestAttempt(route, options) {
72
+ return attempt(() => this.request(route, options));
70
73
  }
71
74
  /**
72
75
  * Create a new WebSocket connection to the server with the given path. The connection will
73
76
  * automatically reconnect if the connection is closed unexpectedly.
74
77
  *
75
- * @param name The path to connect to.
78
+ * @param channel The path to connect to.
76
79
  * @param options The options to pass to the connection.
77
80
  * @returns The WebSocket connection.
78
81
  */
79
- // public connect<P extends RouteName<T>>(name: P, options: Partial<ConnectOptions<T, P>> = {}): WebSocketConnection<T, P> {
80
- // return connect<T, P>(name, { baseUrl: this.baseUrl, ...options })
81
- // }
82
+ connect(channel, options) {
83
+ return connect(channel, { baseUrl: this.options.baseUrl, ...options });
84
+ }
82
85
  }
83
86
  function createClient(options) {
84
87
  return new Client(options);
@@ -1 +1 @@
1
- {"version":3,"file":"createClient.js","sources":["../createClient.ts"],"sourcesContent":["import type { Result } from '@unshared/functions/attempt'\nimport type { ObjectLike } from '@unshared/types'\nimport type { ServiceOptions } from './createService'\nimport type { OpenAPIRoutes } from './openapi'\nimport type { RequestOptions } from './utils/request'\nimport { attempt } from '@unshared/functions/attempt'\nimport { fetch } from './utils/fetch'\nimport { request } from './utils/request'\n\n/** Define the routes that can be fetched from the API and their related options. */\nexport type ClientRoutes = Record<string, RequestOptions>\n\n/** The route name that can be fetched from the API. */\ntype Route<T extends ClientRoutes> =\n T extends Record<infer P extends string, RequestOptions> ? P : string\n\n/** The options to pass to the request based on the route name. */\ntype Options<T extends ClientRoutes, P extends keyof T> =\n T extends Record<P, infer R> ? R : RequestOptions\n\n/** The data returned from the API based on the route name. */\ntype Data<T extends ClientRoutes, P extends keyof T> =\n Options<T, P> extends RequestOptions<any, any, any, any, any, any, infer R extends ObjectLike, any>\n ? R\n : unknown\n\nexport class Client<T extends ClientRoutes = ClientRoutes> {\n\n /**\n * Create a new client for the application.\n *\n * @param initialOptions The options to pass to the client.\n * @example new Client({ baseUrl: 'https://api.example.com' })\n */\n constructor(private initialOptions: 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 async fetch<P extends Route<T>>(route: P, options?: Options<T, P>): Promise<Response> {\n return await fetch(route, { ...this.initialOptions, ...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 async request<P extends Route<T>>(route: P, options?: Options<T, P>): Promise<Data<T, P>> {\n return await request(route, { ...this.initialOptions, ...options }) as Data<T, P>\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 name 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 async requestAttempt<P extends Route<T>>(name: P, options?: Options<T, P>): Promise<Result<Data<T, P>>> {\n return await attempt(() => this.request<P>(name, 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 name 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 RouteName<T>>(name: P, options: Partial<ConnectOptions<T, P>> = {}): WebSocketConnection<T, P> {\n // return connect<T, P>(name, { baseUrl: this.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 ClientRoutes>(options?: RequestOptions): Client<T>\nexport function createClient<T extends { swagger: string }>(options?: ServiceOptions<T>): Client<OpenAPIRoutes<T>>\nexport function createClient<T extends { openapi: string }>(options?: ServiceOptions<T>): Client<OpenAPIRoutes<T>>\nexport function createClient(options?: RequestOptions): Client {\n return new Client(options)\n}\n"],"names":[],"mappings":";;;;AA0BO,MAAM,OAA8C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQzD,YAAoB,iBAAiC,IAAI;AAArC,SAAA,iBAAA;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWpB,MAAa,MAA0B,OAAU,SAA4C;AACpF,WAAA,MAAM,MAAM,OAAO,EAAE,GAAG,KAAK,gBAAgB,GAAG,SAAS;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAqBlE,MAAa,QAA4B,OAAU,SAA8C;AACxF,WAAA,MAAM,QAAQ,OAAO,EAAE,GAAG,KAAK,gBAAgB,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,EAuBpE,MAAa,eAAmC,MAAS,SAAsD;AAC7G,WAAO,MAAM,QAAQ,MAAM,KAAK,QAAW,MAAM,OAAO,CAAC;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAc7D;AAqBO,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 { 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, V extends T[K]>(route: K, options?: V): 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, V extends T[K]>(route: K, options?: V): Promise<Data<V>> {\n return request(route, { ...this.options, ...options }) as Promise<Data<V>>\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, V extends T[K]>(route: K, options?: V): Promise<Result<Data<V>>> {\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, V extends U[P]>(channel: P, options?: V): Awaitable<WebSocketChannel<V>, WebSocketChannel<V>> {\n return connect(channel, { baseUrl: this.options.baseUrl, ...options }) as Awaitable<WebSocketChannel<V>, WebSocketChannel<V>>\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,MAAkD,OAAU,SAAgC;AAC1F,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,QAAoD,OAAU,SAA+B;AAC3F,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,eAA2D,OAAU,SAAuC;AACjH,WAAO,QAAQ,MAAM,KAAK,QAAQ,OAAO,OAAO,CAAC;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAW5C,QAAoD,SAAY,SAAkE;AAChI,WAAA,QAAQ,SAAS,EAAE,SAAS,KAAK,QAAQ,SAAS,GAAG,SAAS;AAAA,EAAA;AAEzE;AAqCO,SAAS,aAAa,SAAkC;AACtD,SAAA,IAAI,OAAO,OAAO;AAC3B;"}
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
- var resolveOperation = require("./chunks/CtW2aMuA.cjs"), handleResponse = require("./chunks/CfKxYeRr.cjs");
2
+ var resolveOperation = require("./chunks/CtW2aMuA.cjs"), handleResponse = require("./chunks/Cayg8606.cjs");
3
+ require("./chunks/CYmaYL5B.cjs");
3
4
  require("@unshared/functions/awaitable");
4
5
  function createService(document, initialOptions) {
5
6
  return new Proxy({}, {
@@ -1 +1 @@
1
- {"version":3,"file":"createService.cjs","sources":["../createService.ts"],"sourcesContent":["import type { MaybeLiteral } from '@unshared/types'\nimport type { OpenAPIV3, Operation, OperationById, OperationId, OperationOptions, OperationResult, ServerUrl } from './openapi'\nimport type { RequestOptions } from './utils/request'\nimport { getServerUrl } from './openapi/getServerUrl'\nimport { resolveOperation } from './openapi/resolveOperation'\nimport { fetch } from './utils/fetch'\nimport { handleResponse } from './utils/handleResponse'\n\n/** A service instance for the given OpenAPI specification. */\nexport type Service<T> = {\n [K in OperationId<T>]:\n OperationById<T, K> extends infer U extends OperationById<T, K>\n ? (data: OperationOptions<T, U>['data'], options?: OperationOptions<T, U>) => Promise<OperationResult<T, U>>\n : never\n}\n\n/** The options to pass to the service client. */\nexport interface ServiceOptions<T = any> extends RequestOptions {\n baseUrl?: MaybeLiteral<ServerUrl<T>>\n headers?: T extends { openapi: string } ? OpenAPIV3.ServerHeaders<T> : never\n}\n\n/**\n * Create a new client instance for the given OpenAPI specification.\n *\n * @param document The OpenAPI specification document.\n * @param initialOptions The initial options to use for every request.\n * @returns The client instance.\n * @example\n *\n * // Import the Giphy OpenAPI specification.\n * import Giphy from './giphy.openapi.json'\n *\n * // Create a new service instance for the Giphy API.\n * const service = createService(Giphy, { headers: { 'Bearer' } })\n *\n * // Request the \"searchGifs\" operation from the Giphy API.\n * await service.getGifs({ limit: 10, offset: 0, q: 'cats' }) // => { data: [...] }\n */\nexport function createService<T extends object>(document: Readonly<T>, initialOptions?: ServiceOptions<T>): Service<T> {\n return new Proxy({}, {\n get(_, id: OperationId<T>) {\n return async(data: object, options: RequestOptions) => {\n\n // --- Find the operation in the OpenAPI specification.\n const baseUrl = getServerUrl(document)\n const operation = resolveOperation(document, id) as Operation\n\n // --- Fetch the relevant resource from the server.\n const { method, path, responses = {} } = operation\n const response = await fetch(path, { method, baseUrl, data, ...initialOptions, ...options })\n if (response.ok) return handleResponse(response, { ...initialOptions, ...options })\n\n // --- Throw an error if the response was not successful.\n const status = response.status.toString()\n if (status in responses\n && typeof responses[status] === 'object'\n && responses[status] !== null\n && 'description' in responses[status]\n && typeof responses[status].description === 'string')\n throw new Error(responses[status].description)\n\n // --- Throw a generic error if the response was not successful.\n throw new Error(response.statusText)\n }\n },\n }) as Service<T>\n}\n"],"names":["getServerUrl","resolveOperation","fetch","handleResponse"],"mappings":";;;AAuCgB,SAAA,cAAgC,UAAuB,gBAAgD;AAC9G,SAAA,IAAI,MAAM,IAAI;AAAA,IACnB,IAAI,GAAG,IAAoB;AAClB,aAAA,OAAM,MAAc,YAA4B;AAGrD,cAAM,UAAUA,iBAAAA,aAAa,QAAQ,GAC/B,YAAYC,iBAAAA,iBAAiB,UAAU,EAAE,GAGzC,EAAE,QAAQ,MAAM,YAAY,GAAG,IAAI,WACnC,WAAW,MAAMC,eAAAA,MAAM,MAAM,EAAE,QAAQ,SAAS,MAAM,GAAG,gBAAgB,GAAG,SAAS;AACvF,YAAA,SAAS,GAAI,QAAOC,eAAe,eAAA,UAAU,EAAE,GAAG,gBAAgB,GAAG,SAAS;AAG5E,cAAA,SAAS,SAAS,OAAO,SAAS;AACxC,cAAI,UAAU,aACT,OAAO,UAAU,MAAM,KAAM,YAC7B,UAAU,MAAM,MAAM,QACtB,iBAAiB,UAAU,MAAM,KACjC,OAAO,UAAU,MAAM,EAAE,eAAgB,WACtC,IAAI,MAAM,UAAU,MAAM,EAAE,WAAW,IAGzC,IAAI,MAAM,SAAS,UAAU;AAAA,MACrC;AAAA,IAAA;AAAA,EACF,CACD;AACH;;"}
1
+ {"version":3,"file":"createService.cjs","sources":["../createService.ts"],"sourcesContent":["import type { MaybeLiteral } from '@unshared/types'\nimport type { OpenAPIV3, Operation, OperationById, OperationId, OperationOptions, OperationResult, ServerUrl } from './openapi'\nimport type { RequestOptions } from './utils/request'\nimport { getServerUrl } from './openapi/getServerUrl'\nimport { resolveOperation } from './openapi/resolveOperation'\nimport { fetch } from './utils/fetch'\nimport { handleResponse } from './utils/handleResponse'\n\n/** A service instance for the given OpenAPI specification. */\nexport type Service<T> = {\n [K in OperationId<T>]:\n OperationById<T, K> extends infer U extends OperationById<T, K>\n ? (data: OperationOptions<T, U>['data'], options?: OperationOptions<T, U>) => Promise<OperationResult<T, U>>\n : never\n}\n\n/** The options to pass to the service client. */\nexport interface ServiceOptions<T = any> extends RequestOptions {\n baseUrl?: MaybeLiteral<ServerUrl<T>>\n headers?: T extends { openapi: string } ? OpenAPIV3.ServerHeaders<T> : never\n}\n\n/**\n * Create a new client instance for the given OpenAPI specification.\n *\n * @param document The OpenAPI specification document.\n * @param initialOptions The initial options to use for every request.\n * @returns The client instance.\n * @example\n *\n * // Import the Giphy OpenAPI specification.\n * import Giphy from './giphy.openapi.json'\n *\n * // Create a new service instance for the Giphy API.\n * const service = createService(Giphy, { headers: { 'Bearer' } })\n *\n * // Request the \"searchGifs\" operation from the Giphy API.\n * await service.getGifs({ limit: 10, offset: 0, q: 'cats' }) // => { data: [...] }\n */\nexport function createService<T extends object>(document: Readonly<T>, initialOptions?: ServiceOptions<T>): Service<T> {\n return new Proxy({}, {\n get(_, id: OperationId<T>) {\n return async(data: object, options: RequestOptions) => {\n\n // --- Find the operation in the OpenAPI specification.\n const baseUrl = getServerUrl(document)\n const operation = resolveOperation(document, id) as Operation\n\n // --- Fetch the relevant resource from the server.\n const { method, path, responses = {} } = operation\n const response = await fetch(path, { method, baseUrl, data, ...initialOptions, ...options })\n if (response.ok) return handleResponse(response, { ...initialOptions, ...options })\n\n // --- Throw an error if the response was not successful.\n const status = response.status.toString()\n if (status in responses\n && typeof responses[status] === 'object'\n && responses[status] !== null\n && 'description' in responses[status]\n && typeof responses[status].description === 'string')\n throw new Error(responses[status].description)\n\n // --- Throw a generic error if the response was not successful.\n throw new Error(response.statusText)\n }\n },\n }) as Service<T>\n}\n"],"names":["getServerUrl","resolveOperation","fetch","handleResponse"],"mappings":";;;;AAuCgB,SAAA,cAAgC,UAAuB,gBAAgD;AAC9G,SAAA,IAAI,MAAM,IAAI;AAAA,IACnB,IAAI,GAAG,IAAoB;AAClB,aAAA,OAAM,MAAc,YAA4B;AAGrD,cAAM,UAAUA,iBAAAA,aAAa,QAAQ,GAC/B,YAAYC,iBAAAA,iBAAiB,UAAU,EAAE,GAGzC,EAAE,QAAQ,MAAM,YAAY,GAAG,IAAI,WACnC,WAAW,MAAMC,eAAAA,MAAM,MAAM,EAAE,QAAQ,SAAS,MAAM,GAAG,gBAAgB,GAAG,SAAS;AACvF,YAAA,SAAS,GAAI,QAAOC,eAAe,eAAA,UAAU,EAAE,GAAG,gBAAgB,GAAG,SAAS;AAG5E,cAAA,SAAS,SAAS,OAAO,SAAS;AACxC,cAAI,UAAU,aACT,OAAO,UAAU,MAAM,KAAM,YAC7B,UAAU,MAAM,MAAM,QACtB,iBAAiB,UAAU,MAAM,KACjC,OAAO,UAAU,MAAM,EAAE,eAAgB,WACtC,IAAI,MAAM,UAAU,MAAM,EAAE,WAAW,IAGzC,IAAI,MAAM,SAAS,UAAU;AAAA,MACrC;AAAA,IAAA;AAAA,EACF,CACD;AACH;;"}
@@ -1,6 +1,6 @@
1
1
  import { MaybeLiteral } from '@unshared/types';
2
- import { b as OperationId, d as OperationById, e as OperationOptions, f as OperationResult, S as ServerUrl, a as OpenAPIV3 } from './chunks/CjU0376e.js';
3
- import { c as RequestOptions } from './chunks/B92aAMq0.js';
2
+ import { b as OperationId, c as OperationById, d as OperationOptions, e as OperationResult, S as ServerUrl, f as OpenAPIV3 } from './chunks/mHfUAX_g.js';
3
+ import { R as RequestOptions } from './chunks/CO11DuYE.js';
4
4
  import 'openapi-types';
5
5
  import './HttpHeaders.js';
6
6
  import './HttpMethods.js';
@@ -1,5 +1,6 @@
1
1
  import { g as getServerUrl, r as resolveOperation } from "./chunks/Biic1J5b.js";
2
- import { f as fetch, h as handleResponse } from "./chunks/D51s1VII.js";
2
+ import { f as fetch, h as handleResponse } from "./chunks/BUeqbyph.js";
3
+ import "./chunks/0ZzUT3m_.js";
3
4
  import "@unshared/functions/awaitable";
4
5
  function createService(document, initialOptions) {
5
6
  return new Proxy({}, {
@@ -1 +1 @@
1
- {"version":3,"file":"createService.js","sources":["../createService.ts"],"sourcesContent":["import type { MaybeLiteral } from '@unshared/types'\nimport type { OpenAPIV3, Operation, OperationById, OperationId, OperationOptions, OperationResult, ServerUrl } from './openapi'\nimport type { RequestOptions } from './utils/request'\nimport { getServerUrl } from './openapi/getServerUrl'\nimport { resolveOperation } from './openapi/resolveOperation'\nimport { fetch } from './utils/fetch'\nimport { handleResponse } from './utils/handleResponse'\n\n/** A service instance for the given OpenAPI specification. */\nexport type Service<T> = {\n [K in OperationId<T>]:\n OperationById<T, K> extends infer U extends OperationById<T, K>\n ? (data: OperationOptions<T, U>['data'], options?: OperationOptions<T, U>) => Promise<OperationResult<T, U>>\n : never\n}\n\n/** The options to pass to the service client. */\nexport interface ServiceOptions<T = any> extends RequestOptions {\n baseUrl?: MaybeLiteral<ServerUrl<T>>\n headers?: T extends { openapi: string } ? OpenAPIV3.ServerHeaders<T> : never\n}\n\n/**\n * Create a new client instance for the given OpenAPI specification.\n *\n * @param document The OpenAPI specification document.\n * @param initialOptions The initial options to use for every request.\n * @returns The client instance.\n * @example\n *\n * // Import the Giphy OpenAPI specification.\n * import Giphy from './giphy.openapi.json'\n *\n * // Create a new service instance for the Giphy API.\n * const service = createService(Giphy, { headers: { 'Bearer' } })\n *\n * // Request the \"searchGifs\" operation from the Giphy API.\n * await service.getGifs({ limit: 10, offset: 0, q: 'cats' }) // => { data: [...] }\n */\nexport function createService<T extends object>(document: Readonly<T>, initialOptions?: ServiceOptions<T>): Service<T> {\n return new Proxy({}, {\n get(_, id: OperationId<T>) {\n return async(data: object, options: RequestOptions) => {\n\n // --- Find the operation in the OpenAPI specification.\n const baseUrl = getServerUrl(document)\n const operation = resolveOperation(document, id) as Operation\n\n // --- Fetch the relevant resource from the server.\n const { method, path, responses = {} } = operation\n const response = await fetch(path, { method, baseUrl, data, ...initialOptions, ...options })\n if (response.ok) return handleResponse(response, { ...initialOptions, ...options })\n\n // --- Throw an error if the response was not successful.\n const status = response.status.toString()\n if (status in responses\n && typeof responses[status] === 'object'\n && responses[status] !== null\n && 'description' in responses[status]\n && typeof responses[status].description === 'string')\n throw new Error(responses[status].description)\n\n // --- Throw a generic error if the response was not successful.\n throw new Error(response.statusText)\n }\n },\n }) as Service<T>\n}\n"],"names":[],"mappings":";;;AAuCgB,SAAA,cAAgC,UAAuB,gBAAgD;AAC9G,SAAA,IAAI,MAAM,IAAI;AAAA,IACnB,IAAI,GAAG,IAAoB;AAClB,aAAA,OAAM,MAAc,YAA4B;AAGrD,cAAM,UAAU,aAAa,QAAQ,GAC/B,YAAY,iBAAiB,UAAU,EAAE,GAGzC,EAAE,QAAQ,MAAM,YAAY,GAAG,IAAI,WACnC,WAAW,MAAM,MAAM,MAAM,EAAE,QAAQ,SAAS,MAAM,GAAG,gBAAgB,GAAG,SAAS;AACvF,YAAA,SAAS,GAAI,QAAO,eAAe,UAAU,EAAE,GAAG,gBAAgB,GAAG,SAAS;AAG5E,cAAA,SAAS,SAAS,OAAO,SAAS;AACxC,cAAI,UAAU,aACT,OAAO,UAAU,MAAM,KAAM,YAC7B,UAAU,MAAM,MAAM,QACtB,iBAAiB,UAAU,MAAM,KACjC,OAAO,UAAU,MAAM,EAAE,eAAgB,WACtC,IAAI,MAAM,UAAU,MAAM,EAAE,WAAW,IAGzC,IAAI,MAAM,SAAS,UAAU;AAAA,MACrC;AAAA,IAAA;AAAA,EACF,CACD;AACH;"}
1
+ {"version":3,"file":"createService.js","sources":["../createService.ts"],"sourcesContent":["import type { MaybeLiteral } from '@unshared/types'\nimport type { OpenAPIV3, Operation, OperationById, OperationId, OperationOptions, OperationResult, ServerUrl } from './openapi'\nimport type { RequestOptions } from './utils/request'\nimport { getServerUrl } from './openapi/getServerUrl'\nimport { resolveOperation } from './openapi/resolveOperation'\nimport { fetch } from './utils/fetch'\nimport { handleResponse } from './utils/handleResponse'\n\n/** A service instance for the given OpenAPI specification. */\nexport type Service<T> = {\n [K in OperationId<T>]:\n OperationById<T, K> extends infer U extends OperationById<T, K>\n ? (data: OperationOptions<T, U>['data'], options?: OperationOptions<T, U>) => Promise<OperationResult<T, U>>\n : never\n}\n\n/** The options to pass to the service client. */\nexport interface ServiceOptions<T = any> extends RequestOptions {\n baseUrl?: MaybeLiteral<ServerUrl<T>>\n headers?: T extends { openapi: string } ? OpenAPIV3.ServerHeaders<T> : never\n}\n\n/**\n * Create a new client instance for the given OpenAPI specification.\n *\n * @param document The OpenAPI specification document.\n * @param initialOptions The initial options to use for every request.\n * @returns The client instance.\n * @example\n *\n * // Import the Giphy OpenAPI specification.\n * import Giphy from './giphy.openapi.json'\n *\n * // Create a new service instance for the Giphy API.\n * const service = createService(Giphy, { headers: { 'Bearer' } })\n *\n * // Request the \"searchGifs\" operation from the Giphy API.\n * await service.getGifs({ limit: 10, offset: 0, q: 'cats' }) // => { data: [...] }\n */\nexport function createService<T extends object>(document: Readonly<T>, initialOptions?: ServiceOptions<T>): Service<T> {\n return new Proxy({}, {\n get(_, id: OperationId<T>) {\n return async(data: object, options: RequestOptions) => {\n\n // --- Find the operation in the OpenAPI specification.\n const baseUrl = getServerUrl(document)\n const operation = resolveOperation(document, id) as Operation\n\n // --- Fetch the relevant resource from the server.\n const { method, path, responses = {} } = operation\n const response = await fetch(path, { method, baseUrl, data, ...initialOptions, ...options })\n if (response.ok) return handleResponse(response, { ...initialOptions, ...options })\n\n // --- Throw an error if the response was not successful.\n const status = response.status.toString()\n if (status in responses\n && typeof responses[status] === 'object'\n && responses[status] !== null\n && 'description' in responses[status]\n && typeof responses[status].description === 'string')\n throw new Error(responses[status].description)\n\n // --- Throw a generic error if the response was not successful.\n throw new Error(response.statusText)\n }\n },\n }) as Service<T>\n}\n"],"names":[],"mappings":";;;;AAuCgB,SAAA,cAAgC,UAAuB,gBAAgD;AAC9G,SAAA,IAAI,MAAM,IAAI;AAAA,IACnB,IAAI,GAAG,IAAoB;AAClB,aAAA,OAAM,MAAc,YAA4B;AAGrD,cAAM,UAAU,aAAa,QAAQ,GAC/B,YAAY,iBAAiB,UAAU,EAAE,GAGzC,EAAE,QAAQ,MAAM,YAAY,GAAG,IAAI,WACnC,WAAW,MAAM,MAAM,MAAM,EAAE,QAAQ,SAAS,MAAM,GAAG,gBAAgB,GAAG,SAAS;AACvF,YAAA,SAAS,GAAI,QAAO,eAAe,UAAU,EAAE,GAAG,gBAAgB,GAAG,SAAS;AAG5E,cAAA,SAAS,SAAS,OAAO,SAAS;AACxC,cAAI,UAAU,aACT,OAAO,UAAU,MAAM,KAAM,YAC7B,UAAU,MAAM,MAAM,QACtB,iBAAiB,UAAU,MAAM,KACjC,OAAO,UAAU,MAAM,EAAE,eAAgB,WACtC,IAAI,MAAM,UAAU,MAAM,EAAE,WAAW,IAGzC,IAAI,MAAM,SAAS,UAAU;AAAA,MACrC;AAAA,IAAA;AAAA,EACF,CACD;AACH;"}
package/dist/index.cjs CHANGED
@@ -1,32 +1,17 @@
1
1
  "use strict";
2
- var createClient = require("./createClient.cjs"), createService = require("./createService.cjs"), HttpHeaders = require("./HttpHeaders.cjs"), HttpMethods = require("./HttpMethods.cjs"), HttpStatusCodes = require("./HttpStatusCodes.cjs"), openapi = require("./openapi.cjs");
3
- require("./utils.cjs");
4
- var resolveOperation = require("./chunks/CtW2aMuA.cjs"), handleResponse = require("./chunks/CfKxYeRr.cjs"), request = require("./chunks/iA98-4f5.cjs");
2
+ var createClient = require("./createClient.cjs"), createService = require("./createService.cjs"), HttpHeaders = require("./HttpHeaders.cjs"), HttpMethods = require("./HttpMethods.cjs"), HttpStatusCodes = require("./HttpStatusCodes.cjs");
5
3
  require("@unshared/functions/attempt");
4
+ require("./chunks/Cayg8606.cjs");
5
+ require("./chunks/CYmaYL5B.cjs");
6
6
  require("@unshared/functions/awaitable");
7
+ require("./chunks/Du_W5H6e.cjs");
8
+ require("./chunks/BMbsYNBd.cjs");
9
+ require("@unshared/functions");
10
+ require("./chunks/CtW2aMuA.cjs");
7
11
  exports.Client = createClient.Client;
8
12
  exports.createClient = createClient.createClient;
9
13
  exports.createService = createService.createService;
10
14
  exports.HttpHeader = HttpHeaders.HttpHeader;
11
15
  exports.HttpMethod = HttpMethods.HttpMethod;
12
16
  exports.HttpStatusCode = HttpStatusCodes.HttpStatusCode;
13
- exports.isReferenceObject = openapi.isReferenceObject;
14
- exports.resolveDocument = openapi.resolveDocument;
15
- exports.resolveReference = openapi.resolveReference;
16
- exports.getServerUrl = resolveOperation.getServerUrl;
17
- exports.resolveOperation = resolveOperation.resolveOperation;
18
- exports.fetch = handleResponse.fetch;
19
- exports.handleResponse = handleResponse.handleResponse;
20
- exports.handleResponseStreamJson = handleResponse.handleResponseStreamJson;
21
- exports.isFormDataLike = handleResponse.isFormDataLike;
22
- exports.isObjectLike = handleResponse.isObjectLike;
23
- exports.parseRequest = handleResponse.parseRequest;
24
- exports.parseRequestBody = handleResponse.parseRequestBody;
25
- exports.parseRequestHeaders = handleResponse.parseRequestHeaders;
26
- exports.parseRequestParameters = handleResponse.parseRequestParameters;
27
- exports.parseRequestQuery = handleResponse.parseRequestQuery;
28
- exports.parseRequestUrl = handleResponse.parseRequestUrl;
29
- exports.toFormData = handleResponse.toFormData;
30
- exports.toSearchParams = handleResponse.toSearchParams;
31
- exports.request = request.request;
32
17
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;"}
package/dist/index.d.ts CHANGED
@@ -1,13 +1,12 @@
1
- export { Client, ClientRoutes, createClient } from './createClient.js';
1
+ export { Client, createClient } from './createClient.js';
2
2
  export { Service, ServiceOptions, createService } from './createService.js';
3
3
  export { HttpHeader } from './HttpHeaders.js';
4
4
  export { HttpMethod } from './HttpMethods.js';
5
5
  export { HttpStatusCode } from './HttpStatusCodes.js';
6
- export { k as OpenAPIRoutes, O as OpenAPIV2, a as OpenAPIV3, c as Operation, d as OperationById, j as OperationByRoute, b as OperationId, e as OperationOptions, h as OperationResponse, f as OperationResult, i as OperationRoute, S as ServerUrl, g as getServerUrl, r as resolveOperation } from './chunks/CjU0376e.js';
7
- export { OpenAPIReference, OpenAPIReferenceDecoded, OpenAPIReferencePath, OpenAPIReferenceResolved, OpenAPIResolved, isReferenceObject, resolveDocument, resolveReference } from './openapi.js';
8
- export { FormDataLike, fetch, handleResponse, handleResponseStreamJson, isFormDataLike, isObjectLike, parseRequestBody, parseRequestHeaders, parseRequestParameters, parseRequestQuery, parseRequestUrl, toFormData } from './utils.js';
9
- export { a as FetchHeaders, F as FetchMethod, b as FetchOptions, R as RequestContext, c as RequestOptions, d as SearchArrayFormat, S as SearchParamsObject, T as ToSearchParamsOptions, p as parseRequest, r as request, t as toSearchParams } from './chunks/B92aAMq0.js';
6
+ import '@unshared/functions';
10
7
  import '@unshared/functions/attempt';
8
+ import './chunks/mHfUAX_g.js';
11
9
  import '@unshared/types';
10
+ import './chunks/CO11DuYE.js';
12
11
  import 'openapi-types';
13
- import '@unshared/functions/awaitable';
12
+ import './websocket.js';
package/dist/index.js CHANGED
@@ -3,38 +3,20 @@ import { createService } from "./createService.js";
3
3
  import { HttpHeader } from "./HttpHeaders.js";
4
4
  import { HttpMethod } from "./HttpMethods.js";
5
5
  import { HttpStatusCode } from "./HttpStatusCodes.js";
6
- import { isReferenceObject, resolveDocument, resolveReference } from "./openapi.js";
7
- import "./utils.js";
8
- import { g, r } from "./chunks/Biic1J5b.js";
9
- import { f, h, a, i, b, p, c, d, e, g as g2, j, t, k } from "./chunks/D51s1VII.js";
10
- import { r as r2 } from "./chunks/lMH6B5BV.js";
11
6
  import "@unshared/functions/attempt";
7
+ import "./chunks/BUeqbyph.js";
8
+ import "./chunks/0ZzUT3m_.js";
12
9
  import "@unshared/functions/awaitable";
10
+ import "./chunks/DJJsADWD.js";
11
+ import "./chunks/6IxvRpmY.js";
12
+ import "@unshared/functions";
13
+ import "./chunks/Biic1J5b.js";
13
14
  export {
14
15
  Client,
15
16
  HttpHeader,
16
17
  HttpMethod,
17
18
  HttpStatusCode,
18
19
  createClient,
19
- createService,
20
- f as fetch,
21
- g as getServerUrl,
22
- h as handleResponse,
23
- a as handleResponseStreamJson,
24
- i as isFormDataLike,
25
- b as isObjectLike,
26
- isReferenceObject,
27
- p as parseRequest,
28
- c as parseRequestBody,
29
- d as parseRequestHeaders,
30
- e as parseRequestParameters,
31
- g2 as parseRequestQuery,
32
- j as parseRequestUrl,
33
- r2 as request,
34
- resolveDocument,
35
- r as resolveOperation,
36
- resolveReference,
37
- t as toFormData,
38
- k as toSearchParams
20
+ createService
39
21
  };
40
22
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;"}
package/dist/openapi.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- export { k as OpenAPIRoutes, O as OpenAPIV2, a as OpenAPIV3, c as Operation, d as OperationById, j as OperationByRoute, b as OperationId, e as OperationOptions, h as OperationResponse, f as OperationResult, i as OperationRoute, S as ServerUrl, g as getServerUrl, r as resolveOperation } from './chunks/CjU0376e.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/mHfUAX_g.js';
2
2
  import { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types';
3
3
  import { StringReplace, WriteableDeep, StringJoin, Substract } from '@unshared/types';
4
- import './chunks/B92aAMq0.js';
4
+ import './chunks/CO11DuYE.js';
5
5
  import './HttpHeaders.js';
6
6
  import './HttpMethods.js';
7
7
 
package/dist/utils.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- var handleResponse = require("./chunks/CfKxYeRr.cjs"), request = require("./chunks/iA98-4f5.cjs");
2
+ var handleResponse = require("./chunks/Cayg8606.cjs"), parseRequestQuery = require("./chunks/CYmaYL5B.cjs"), request = require("./chunks/Du_W5H6e.cjs");
3
3
  require("@unshared/functions/awaitable");
4
4
  exports.fetch = handleResponse.fetch;
5
5
  exports.handleResponse = handleResponse.handleResponse;
@@ -9,10 +9,10 @@ exports.isObjectLike = handleResponse.isObjectLike;
9
9
  exports.parseRequest = handleResponse.parseRequest;
10
10
  exports.parseRequestBody = handleResponse.parseRequestBody;
11
11
  exports.parseRequestHeaders = handleResponse.parseRequestHeaders;
12
- exports.parseRequestParameters = handleResponse.parseRequestParameters;
13
- exports.parseRequestQuery = handleResponse.parseRequestQuery;
14
12
  exports.parseRequestUrl = handleResponse.parseRequestUrl;
15
13
  exports.toFormData = handleResponse.toFormData;
16
- exports.toSearchParams = handleResponse.toSearchParams;
14
+ exports.parseRequestParameters = parseRequestQuery.parseRequestParameters;
15
+ exports.parseRequestQuery = parseRequestQuery.parseRequestQuery;
16
+ exports.toSearchParams = parseRequestQuery.toSearchParams;
17
17
  exports.request = request.request;
18
18
  //# sourceMappingURL=utils.cjs.map
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { b as FetchOptions, c as RequestOptions, R as RequestContext } from './chunks/B92aAMq0.js';
2
- export { a 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/B92aAMq0.js';
1
+ import { a as FetchOptions, R as RequestOptions, b as RequestContext } from './chunks/CO11DuYE.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/CO11DuYE.js';
3
3
  import { Awaitable } from '@unshared/functions/awaitable';
4
4
  import { ObjectLike } from '@unshared/types';
5
5
  import './HttpHeaders.js';
package/dist/utils.js CHANGED
@@ -1,5 +1,6 @@
1
- import { f, h, a, i, b, p, c, d, e, g, j, t, k } from "./chunks/D51s1VII.js";
2
- import { r } from "./chunks/lMH6B5BV.js";
1
+ import { f, h, a, i, b, p, c, d, e, t } from "./chunks/BUeqbyph.js";
2
+ import { p as p2, a as a2, t as t2 } from "./chunks/0ZzUT3m_.js";
3
+ import { r } from "./chunks/DJJsADWD.js";
3
4
  import "@unshared/functions/awaitable";
4
5
  export {
5
6
  f as fetch,
@@ -10,11 +11,11 @@ export {
10
11
  p as parseRequest,
11
12
  c as parseRequestBody,
12
13
  d as parseRequestHeaders,
13
- e as parseRequestParameters,
14
- g as parseRequestQuery,
15
- j as parseRequestUrl,
14
+ p2 as parseRequestParameters,
15
+ a2 as parseRequestQuery,
16
+ e as parseRequestUrl,
16
17
  r as request,
17
18
  t as toFormData,
18
- k as toSearchParams
19
+ t2 as toSearchParams
19
20
  };
20
21
  //# sourceMappingURL=utils.js.map
package/dist/utils.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
1
+ {"version":3,"file":"utils.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ var connect = require("./chunks/BMbsYNBd.cjs");
3
+ require("@unshared/functions");
4
+ require("./chunks/CYmaYL5B.cjs");
5
+ exports.WebSocketChannel = connect.WebSocketChannel;
6
+ exports.connect = connect.connect;
7
+ exports.parseConnectOptions = connect.parseConnectOptions;
8
+ //# sourceMappingURL=websocket.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;"}
@@ -0,0 +1,143 @@
1
+ import { Awaitable } from '@unshared/functions';
2
+ import { ObjectLike, Loose, UnionMerge } from '@unshared/types';
3
+
4
+ /** The protocols to use for the connection. */
5
+ type ConnectProtocol = 'WS' | 'WSS';
6
+ /** Options to pass to the `createChannel` function. */
7
+ interface ConnectOptions<BaseUrl extends string = string, Query extends ObjectLike = ObjectLike, Parameters extends ObjectLike = ObjectLike, ClientData extends ObjectLike = any, ServerData extends ObjectLike = any> {
8
+ /** The protocol to use when connecting to the server. */
9
+ protocol?: Lowercase<ConnectProtocol> | Uppercase<ConnectProtocol>;
10
+ /** The base URL to connect to. */
11
+ baseUrl?: BaseUrl;
12
+ /**
13
+ * The path parameters to use when connecting to the server. These parameters will be used to
14
+ * fill in the path parameters of the connection URL.
15
+ *
16
+ * @example { id: 1 }
17
+ */
18
+ parameters?: Parameters;
19
+ /**
20
+ * The query parameters to use when connecting to the server. These parameters will be used to
21
+ * fill in the query parameters of the connection URL.
22
+ *
23
+ * @example { limit: 10, offset: 0 }
24
+ */
25
+ query?: Loose<Query>;
26
+ /**
27
+ * The data to send when creating the connection. Namely, the path parameters
28
+ * to use when connecting to the server.
29
+ *
30
+ * @example
31
+ *
32
+ * // Create a new connection to `http://localhost:8080/users/1`.
33
+ * connect('GET /users/:id', {
34
+ * data: { id: 1 },
35
+ * baseUrl: 'http://localhost:8080'
36
+ * })
37
+ */
38
+ data?: UnionMerge<Loose<Query> | Parameters>;
39
+ /**
40
+ * The payload to send when creating the connection. Namely, the initial message
41
+ * to send to the server when the connection is established.
42
+ */
43
+ initialPayload?: Loose<ClientData>;
44
+ /**
45
+ * Weather to reconnect the connection when it is closed unexpectedly. If `true`,
46
+ * the connection will automatically reconnect when it is closed. If `false`, the
47
+ * connection will not reconnect when it is closed.
48
+ *
49
+ * @default false
50
+ */
51
+ autoReconnect?: boolean;
52
+ /**
53
+ * The delay in milliseconds to wait before reconnecting the connection. This delay
54
+ * will be used to wait before reconnecting the connection after it is closed.
55
+ *
56
+ * @default 0
57
+ */
58
+ reconnectDelay?: number;
59
+ /**
60
+ * The maximum number of times to reconnect the connection before giving up. This
61
+ * number will be used to determine when to stop trying to reconnect the connection.
62
+ *
63
+ * @default 3
64
+ */
65
+ reconnectLimit?: number;
66
+ /**
67
+ * The function to call when the connection is opened. This function will be called
68
+ * when the connection is successfully opened or reconnected.
69
+ */
70
+ onOpen?: (event: Event) => void;
71
+ /**
72
+ * The function to call when the connection is closed with an error. This function will
73
+ * be called when the connection is closed unexpectedly with an error.
74
+ */
75
+ onError?: (event: Event) => void;
76
+ /**
77
+ * The function to call when the connection is closed. This function will be called
78
+ * when the connection is closed unexpectedly or when the connection is closed manually.
79
+ */
80
+ onClose?: (event: CloseEvent) => void;
81
+ /**
82
+ * The function to call when a message is received from the server. This function will
83
+ * be called when a message is received from the server.
84
+ */
85
+ onMessage?: (data: ServerData) => void;
86
+ }
87
+ interface WebSocketParameters {
88
+ url: URL;
89
+ protocol?: 'ws' | 'wss';
90
+ }
91
+ declare function parseConnectOptions(channel: string, options: ConnectOptions): WebSocketParameters;
92
+
93
+ type RemoveListener = () => void;
94
+ type ClientData<T extends ConnectOptions> = T extends ConnectOptions<any, any, any, infer R, any> ? R : any;
95
+ type ServerData<T extends ConnectOptions> = T extends ConnectOptions<any, any, any, any, infer R> ? R : any;
96
+ declare class WebSocketChannel<T extends ConnectOptions = ConnectOptions> {
97
+ channel: string;
98
+ options: T;
99
+ constructor(channel: string, options: T);
100
+ /** The WebSocket connection to the server. */
101
+ webSocket: undefined | WebSocket;
102
+ /**
103
+ * Open a new WebSocket connection to the server. The connection will be opened with the given
104
+ * URL and protocols. If the connection is already open, the connection will be closed before
105
+ * opening a new connection. Also add the event listeners that were passed in the options.
106
+ *
107
+ * @returns The WebSocket connection.
108
+ */
109
+ open(): Promise<this>;
110
+ /**
111
+ * Send a payload to the server. The payload will be serialized to JSON before sending.
112
+ *
113
+ * @param payload The data to send to the server.
114
+ */
115
+ send(payload: ClientData<T>): void;
116
+ /**
117
+ * Listen for events from the server. The event will be deserialized from JSON before calling the callback.
118
+ *
119
+ * @param event The event to listen for.
120
+ * @param callback The callback to call when the event is received.
121
+ * @returns A function to remove the event listener.
122
+ */
123
+ on(event: 'message', callback: (data: ServerData<T>) => void, options?: AddEventListenerOptions): RemoveListener;
124
+ on(event: 'close', callback: (event: CloseEvent) => void, options?: AddEventListenerOptions): RemoveListener;
125
+ on(event: 'error', callback: (event: Event) => void, options?: AddEventListenerOptions): RemoveListener;
126
+ on(event: 'open', callback: (event: Event) => void, options?: AddEventListenerOptions): RemoveListener;
127
+ /**
128
+ * Close the WebSocket connection to the server. The connection will not be able to send or receive
129
+ * messages after it is closed.
130
+ */
131
+ close(): Promise<void>;
132
+ }
133
+ /**
134
+ * Create a new WebSocket connection to the server with the given path. The connection will
135
+ * automatically reconnect if the connection is closed unexpectedly.
136
+ *
137
+ * @param route The name of the route to connect to.
138
+ * @param options The options to pass to the connection.
139
+ * @returns The WebSocket connection.
140
+ */
141
+ declare function connect(route: string, options: ConnectOptions): Awaitable<WebSocketChannel, WebSocketChannel>;
142
+
143
+ export { type ConnectOptions, type ConnectProtocol, WebSocketChannel, type WebSocketParameters, connect, parseConnectOptions };
@@ -0,0 +1,9 @@
1
+ import { W, c, p } from "./chunks/6IxvRpmY.js";
2
+ import "@unshared/functions";
3
+ import "./chunks/0ZzUT3m_.js";
4
+ export {
5
+ W as WebSocketChannel,
6
+ c as connect,
7
+ p as parseConnectOptions
8
+ };
9
+ //# sourceMappingURL=websocket.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unshared/client",
3
3
  "type": "module",
4
- "version": "0.3.0",
4
+ "version": "0.3.2",
5
5
  "license": "MIT",
6
6
  "sideEffects": false,
7
7
  "author": "Stanley Horwood <stanley@hsjm.io>",
@@ -51,6 +51,11 @@
51
51
  "types": "./dist/utils.d.ts",
52
52
  "import": "./dist/utils.js"
53
53
  },
54
+ "./websocket": {
55
+ "require": "./dist/websocket.cjs",
56
+ "types": "./dist/websocket.d.ts",
57
+ "import": "./dist/websocket.js"
58
+ },
54
59
  ".": {
55
60
  "require": "./dist/index.cjs",
56
61
  "types": "./dist/index.d.ts",
@@ -63,12 +68,12 @@
63
68
  "LICENSE.md"
64
69
  ],
65
70
  "dependencies": {
66
- "@unshared/functions": "0.3.0",
67
- "@unshared/types": "0.3.0",
71
+ "@unshared/functions": "0.3.2",
72
+ "@unshared/types": "0.3.2",
68
73
  "openapi-types": "12.1.3"
69
74
  },
70
75
  "devDependencies": {
71
- "@unshared/scripts": "0.3.0"
76
+ "@unshared/scripts": "0.3.2"
72
77
  },
73
78
  "scripts": {
74
79
  "build:httpMethods": "tsx ./scripts/buildHttpMethods.ts",