@unshared/client 0.6.5 → 0.7.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.
- package/dist/HttpHeaders.cjs +1 -1
- package/dist/HttpHeaders.cjs.map +1 -1
- package/dist/HttpHeaders.d.ts +10145 -1117
- package/dist/HttpHeaders.js +1 -1
- package/dist/HttpHeaders.js.map +1 -1
- package/dist/HttpMethods.cjs.map +1 -1
- package/dist/HttpMethods.d.ts +506 -20
- package/dist/HttpMethods.js.map +1 -1
- package/dist/HttpStatusCodes.cjs.map +1 -1
- package/dist/HttpStatusCodes.d.ts +2539 -83
- package/dist/HttpStatusCodes.js.map +1 -1
- package/dist/chunks/{CThSMMCZ.cjs → -InYnohy.cjs} +41 -50
- package/dist/chunks/{CThSMMCZ.cjs.map → -InYnohy.cjs.map} +1 -1
- package/dist/chunks/B6pUErTM.js.map +1 -1
- package/dist/chunks/BDxlAULu.cjs.map +1 -1
- package/dist/chunks/B_Gz6Yz8.js.map +1 -1
- package/dist/chunks/C83nLcQu.js.map +1 -1
- package/dist/chunks/{CQUndCW0.cjs → CNNxr5Ws.cjs} +8 -4
- package/dist/chunks/CNNxr5Ws.cjs.map +1 -0
- package/dist/chunks/{BGG3bT7O.d.ts → CNqEW-S3.d.ts} +12 -8
- package/dist/chunks/ClHrsaTt.js +19 -0
- package/dist/chunks/ClHrsaTt.js.map +1 -0
- package/dist/chunks/DEyigyGy.cjs.map +1 -1
- package/dist/chunks/DJyo3R5b.cjs.map +1 -1
- package/dist/chunks/{DAtM4FUl.d.ts → DKPTfBgy.d.ts} +1 -1
- package/dist/chunks/{uqfvs89o.js → DZ2MwV7J.js} +41 -50
- package/dist/chunks/{uqfvs89o.js.map → DZ2MwV7J.js.map} +1 -1
- package/dist/createClient.cjs +2 -2
- package/dist/createClient.cjs.map +1 -1
- package/dist/createClient.d.ts +3 -2
- package/dist/createClient.js +2 -2
- package/dist/createClient.js.map +1 -1
- package/dist/createService.cjs +1 -1
- package/dist/createService.cjs.map +1 -1
- package/dist/createService.d.ts +3 -2
- package/dist/createService.js +1 -1
- package/dist/createService.js.map +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -2
- package/dist/openapi.cjs.map +1 -1
- package/dist/openapi.d.ts +20 -2
- package/dist/openapi.js.map +1 -1
- package/dist/utils.cjs +1 -1
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.ts +5 -5
- package/dist/utils.js +3 -3
- package/dist/utils.js.map +1 -1
- package/package.json +4 -4
- package/dist/chunks/CQUndCW0.cjs.map +0 -1
- package/dist/chunks/xJaQKKe2.js +0 -15
- package/dist/chunks/xJaQKKe2.js.map +0 -1
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"createClient.cjs","sources":["../createClient.ts"],"sourcesContent":["import 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
|
1
|
+
{"version":3,"file":"createClient.cjs","sources":["../createClient.ts"],"sourcesContent":["import 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<\n T extends Routes = Routes,\n U extends Channels = Channels,\n> {\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]): WebSocketChannel<U[P]>\n public connect(channel: string, options?: ConnectOptions): WebSocketChannel\n public connect(channel: string, options?: ConnectOptions): 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":";;;;;AAcO,MAAM,OAGX;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,YAAmB,UAA0B,IAAI;AAA9B,SAAA,UAAA;AAAA,EAA+B;AAAA,EAa3C,MAAM,OAAe,SAA2C;AACrE,WAAOA,QAAAA,MAAM,OAAO,EAAE,GAAG,KAAK,SAAS,GAAG,SAAS;AAAA,EACrD;AAAA,EAsBO,QAAQ,OAAe,SAA4C;AACxE,WAAOC,QAAAA,QAAQ,OAAO,EAAE,GAAG,KAAK,SAAS,GAAG,SAAS;AAAA,EACvD;AAAA,EAwBO,eAAe,OAAe,SAAoD;AACvF,WAAOC,QAAAA,QAAQ,MAAM,KAAK,QAAQ,OAAO,OAAO,CAAC;AAAA,EACnD;AAAA,EAYO,QAAQ,SAAiB,SAA4C;AAC1E,WAAOC,QAAAA,QAAQ,SAAS,EAAE,SAAS,KAAK,QAAQ,SAAS,GAAG,SAAS;AAAA,EACvE;AACF;AAqCO,SAAS,aAAa,SAAkC;AAC7D,SAAO,IAAI,OAAO,OAAO;AAC3B;;;"}
|
package/dist/createClient.d.ts
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
import { Result } from '@unshared/functions/attempt';
|
2
2
|
import { ServiceOptions } from './createService.js';
|
3
|
-
import { R as RequestOptions, F as FetchOptions } from './chunks/
|
4
|
-
import { O as OpenAPILike, a as OpenAPIOptionsMap } from './chunks/
|
3
|
+
import { R as RequestOptions, F as FetchOptions } from './chunks/CNqEW-S3.js';
|
4
|
+
import { O as OpenAPILike, a as OpenAPIOptionsMap } from './chunks/DKPTfBgy.js';
|
5
5
|
import { ConnectOptions, WebSocketChannel } from './websocket.js';
|
6
6
|
import '@unshared/types';
|
7
|
+
import '@unshared/functions/awaitable';
|
7
8
|
import './HttpHeaders.js';
|
8
9
|
import './HttpMethods.js';
|
9
10
|
import 'openapi-types';
|
package/dist/createClient.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import { attempt } from "@unshared/functions/attempt";
|
2
|
-
import { f as fetch, r as request } from "./chunks/
|
2
|
+
import { f as fetch, r as request } from "./chunks/ClHrsaTt.js";
|
3
3
|
import { c as connect } from "./chunks/C83nLcQu.js";
|
4
|
-
import "./chunks/
|
4
|
+
import "./chunks/DZ2MwV7J.js";
|
5
5
|
import "./chunks/B6pUErTM.js";
|
6
6
|
import "@unshared/functions/awaitable";
|
7
7
|
class Client {
|
package/dist/createClient.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"createClient.js","sources":["../createClient.ts"],"sourcesContent":["import 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
|
1
|
+
{"version":3,"file":"createClient.js","sources":["../createClient.ts"],"sourcesContent":["import 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<\n T extends Routes = Routes,\n U extends Channels = Channels,\n> {\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]): WebSocketChannel<U[P]>\n public connect(channel: string, options?: ConnectOptions): WebSocketChannel\n public connect(channel: string, options?: ConnectOptions): 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":";;;;;;AAcO,MAAM,OAGX;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,YAAmB,UAA0B,IAAI;AAA9B,SAAA,UAAA;AAAA,EAA+B;AAAA,EAa3C,MAAM,OAAe,SAA2C;AACrE,WAAO,MAAM,OAAO,EAAE,GAAG,KAAK,SAAS,GAAG,SAAS;AAAA,EACrD;AAAA,EAsBO,QAAQ,OAAe,SAA4C;AACxE,WAAO,QAAQ,OAAO,EAAE,GAAG,KAAK,SAAS,GAAG,SAAS;AAAA,EACvD;AAAA,EAwBO,eAAe,OAAe,SAAoD;AACvF,WAAO,QAAQ,MAAM,KAAK,QAAQ,OAAO,OAAO,CAAC;AAAA,EACnD;AAAA,EAYO,QAAQ,SAAiB,SAA4C;AAC1E,WAAO,QAAQ,SAAS,EAAE,SAAS,KAAK,QAAQ,SAAS,GAAG,SAAS;AAAA,EACvE;AACF;AAqCO,SAAS,aAAa,SAAkC;AAC7D,SAAO,IAAI,OAAO,OAAO;AAC3B;"}
|
package/dist/createService.cjs
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
"use strict";
|
2
|
-
var resolveOperationTokenOptions = require("./chunks/DEyigyGy.cjs"), handleResponse = require("./chunks
|
2
|
+
var resolveOperationTokenOptions = require("./chunks/DEyigyGy.cjs"), handleResponse = require("./chunks/-InYnohy.cjs");
|
3
3
|
require("./chunks/BDxlAULu.cjs");
|
4
4
|
require("@unshared/functions/awaitable");
|
5
5
|
function createService(document, initialOptions) {
|
@@ -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 { resolveOperationTokenOptions } from './openapi/resolveOperationTokenOptions'\nimport { handleResponse } from './utils/handleResponse'\nimport { parseRequest } from './utils/parseRequest'\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 const tokenOptions = resolveOperationTokenOptions(document, operation)\n\n // --- Fetch the relevant resource from the server.\n const { method, path, responses = {} } = operation\n const { url, init } = parseRequest(path, { method, baseUrl, data, ...tokenOptions, ...initialOptions, ...options })\n const response = await globalThis.fetch(url, init)\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","resolveOperationTokenOptions","parseRequest","handleResponse"],"mappings":";;;;
|
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 { resolveOperationTokenOptions } from './openapi/resolveOperationTokenOptions'\nimport { handleResponse } from './utils/handleResponse'\nimport { parseRequest } from './utils/parseRequest'\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 const tokenOptions = resolveOperationTokenOptions(document, operation)\n\n // --- Fetch the relevant resource from the server.\n const { method, path, responses = {} } = operation\n const { url, init } = parseRequest(path, { method, baseUrl, data, ...tokenOptions, ...initialOptions, ...options })\n const response = await globalThis.fetch(url, init)\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","resolveOperationTokenOptions","parseRequest","handleResponse"],"mappings":";;;;AAwCO,SAAS,cAAgC,UAAuB,gBAAgD;AACrH,SAAO,IAAI,MAAM,IAAI;AAAA,IACnB,IAAI,GAAG,IAAoB;AACzB,aAAO,OAAM,MAAc,YAA4B;AAGrD,cAAM,UAAUA,6BAAAA,aAAa,QAAQ,GAC/B,YAAYC,8CAAiB,UAAU,EAAE,GACzC,eAAeC,6BAAAA,6BAA6B,UAAU,SAAS,GAG/D,EAAE,QAAQ,MAAM,YAAY,CAAA,MAAO,WACnC,EAAE,KAAK,KAAA,IAASC,eAAAA,aAAa,MAAM,EAAE,QAAQ,SAAS,MAAM,GAAG,cAAc,GAAG,gBAAgB,GAAG,SAAS,GAC5G,WAAW,MAAM,WAAW,MAAM,KAAK,IAAI;AACjD,YAAI,SAAS,GAAI,QAAOC,eAAAA,eAAe,UAAU,EAAE,GAAG,gBAAgB,GAAG,SAAS;AAGlF,cAAM,SAAS,SAAS,OAAO,SAAA;AAC/B,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,IACF;AAAA,EAAA,CACD;AACH;;"}
|
package/dist/createService.d.ts
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
import { MaybeLiteral } from '@unshared/types';
|
2
|
-
import { S as ServerUrl, b as OpenAPIV3, c as OperationId, d as OperationById, e as OperationOptions, f as OperationResult } from './chunks/
|
3
|
-
import { R as RequestOptions } from './chunks/
|
2
|
+
import { S as ServerUrl, b as OpenAPIV3, c as OperationId, d as OperationById, e as OperationOptions, f as OperationResult } from './chunks/DKPTfBgy.js';
|
3
|
+
import { R as RequestOptions } from './chunks/CNqEW-S3.js';
|
4
4
|
import 'openapi-types';
|
5
|
+
import '@unshared/functions/awaitable';
|
5
6
|
import './HttpHeaders.js';
|
6
7
|
import './HttpMethods.js';
|
7
8
|
|
package/dist/createService.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { g as getServerUrl, r as resolveOperation, a as resolveOperationTokenOptions } from "./chunks/B_Gz6Yz8.js";
|
2
|
-
import { p as parseRequest, h as handleResponse } from "./chunks/
|
2
|
+
import { p as parseRequest, h as handleResponse } from "./chunks/DZ2MwV7J.js";
|
3
3
|
import "./chunks/B6pUErTM.js";
|
4
4
|
import "@unshared/functions/awaitable";
|
5
5
|
function createService(document, initialOptions) {
|
@@ -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 { resolveOperationTokenOptions } from './openapi/resolveOperationTokenOptions'\nimport { handleResponse } from './utils/handleResponse'\nimport { parseRequest } from './utils/parseRequest'\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 const tokenOptions = resolveOperationTokenOptions(document, operation)\n\n // --- Fetch the relevant resource from the server.\n const { method, path, responses = {} } = operation\n const { url, init } = parseRequest(path, { method, baseUrl, data, ...tokenOptions, ...initialOptions, ...options })\n const response = await globalThis.fetch(url, init)\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":";;;;
|
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 { resolveOperationTokenOptions } from './openapi/resolveOperationTokenOptions'\nimport { handleResponse } from './utils/handleResponse'\nimport { parseRequest } from './utils/parseRequest'\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 const tokenOptions = resolveOperationTokenOptions(document, operation)\n\n // --- Fetch the relevant resource from the server.\n const { method, path, responses = {} } = operation\n const { url, init } = parseRequest(path, { method, baseUrl, data, ...tokenOptions, ...initialOptions, ...options })\n const response = await globalThis.fetch(url, init)\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":";;;;AAwCO,SAAS,cAAgC,UAAuB,gBAAgD;AACrH,SAAO,IAAI,MAAM,IAAI;AAAA,IACnB,IAAI,GAAG,IAAoB;AACzB,aAAO,OAAM,MAAc,YAA4B;AAGrD,cAAM,UAAU,aAAa,QAAQ,GAC/B,YAAY,iBAAiB,UAAU,EAAE,GACzC,eAAe,6BAA6B,UAAU,SAAS,GAG/D,EAAE,QAAQ,MAAM,YAAY,CAAA,MAAO,WACnC,EAAE,KAAK,KAAA,IAAS,aAAa,MAAM,EAAE,QAAQ,SAAS,MAAM,GAAG,cAAc,GAAG,gBAAgB,GAAG,SAAS,GAC5G,WAAW,MAAM,WAAW,MAAM,KAAK,IAAI;AACjD,YAAI,SAAS,GAAI,QAAO,eAAe,UAAU,EAAE,GAAG,gBAAgB,GAAG,SAAS;AAGlF,cAAM,SAAS,SAAS,OAAO,SAAA;AAC/B,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,IACF;AAAA,EAAA,CACD;AACH;"}
|
package/dist/index.cjs
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
"use strict";
|
2
2
|
var createClient = require("./createClient.cjs"), createService = require("./createService.cjs"), HttpHeaders = require("./HttpHeaders.cjs"), HttpMethods = require("./HttpMethods.cjs"), HttpStatusCodes = require("./HttpStatusCodes.cjs");
|
3
3
|
require("@unshared/functions/attempt");
|
4
|
-
require("./chunks/
|
5
|
-
require("./chunks
|
4
|
+
require("./chunks/CNNxr5Ws.cjs");
|
5
|
+
require("./chunks/-InYnohy.cjs");
|
6
6
|
require("./chunks/BDxlAULu.cjs");
|
7
7
|
require("@unshared/functions/awaitable");
|
8
8
|
require("./chunks/DJyo3R5b.cjs");
|
package/dist/index.d.ts
CHANGED
@@ -4,8 +4,9 @@ export { HttpHeader } from './HttpHeaders.js';
|
|
4
4
|
export { HttpMethod } from './HttpMethods.js';
|
5
5
|
export { HttpStatusCode } from './HttpStatusCodes.js';
|
6
6
|
import '@unshared/functions/attempt';
|
7
|
-
import './chunks/
|
7
|
+
import './chunks/CNqEW-S3.js';
|
8
|
+
import '@unshared/functions/awaitable';
|
8
9
|
import '@unshared/types';
|
9
|
-
import './chunks/
|
10
|
+
import './chunks/DKPTfBgy.js';
|
10
11
|
import 'openapi-types';
|
11
12
|
import './websocket.js';
|
package/dist/index.js
CHANGED
@@ -4,8 +4,8 @@ import { HttpHeader } from "./HttpHeaders.js";
|
|
4
4
|
import { HttpMethod } from "./HttpMethods.js";
|
5
5
|
import { HttpStatusCode } from "./HttpStatusCodes.js";
|
6
6
|
import "@unshared/functions/attempt";
|
7
|
-
import "./chunks/
|
8
|
-
import "./chunks/
|
7
|
+
import "./chunks/ClHrsaTt.js";
|
8
|
+
import "./chunks/DZ2MwV7J.js";
|
9
9
|
import "./chunks/B6pUErTM.js";
|
10
10
|
import "@unshared/functions/awaitable";
|
11
11
|
import "./chunks/C83nLcQu.js";
|
package/dist/openapi.cjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"openapi.cjs","sources":["../openapi/isOpenAPIV2.ts","../openapi/isOpenAPIV3_1.ts","../openapi/isReferenceObject.ts","../openapi/resolveReference.ts","../openapi/resolveDocument.ts"],"sourcesContent":["/* eslint-disable unicorn/filename-case */\nimport type { OpenAPIV2 } from 'openapi-types'\n\n/**\n * Check if the given document is an OpenAPI v2 specification.\n *\n * @param value The document to check.\n * @returns `true` if the document is an OpenAPI v2 specification, `false` otherwise.\n * @example isOpenAPIV2({ swagger: '2.0', info: { title: 'Test API', version: '1.0.0' } }) // => true\n */\nexport function isOpenAPIV2(value: unknown): value is OpenAPIV2.Document {\n return typeof value === 'object'\n && value !== null\n && 'swagger' in value\n && value.swagger === '2.0'\n}\n","/* eslint-disable unicorn/filename-case */\nimport type { OpenAPIV3_1 } from 'openapi-types'\n\n/**\n * Check if the given document is an OpenAPI v3.1 specification.\n *\n * @param value The document to check.\n * @returns `true` if the document is an OpenAPI v3.1 specification, `false` otherwise.\n * @example isOpenAPIV3_1({ openapi: '3.1.0', info: { title: 'Test API', version: '1.0.0' } }) // => true\n */\nexport function isOpenAPIV3_1(value: unknown): value is OpenAPIV3_1.Document {\n return typeof value === 'object'\n && value !== null\n && 'openapi' in value\n && value.openapi === '3.1.0'\n}\n","import type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport type OpenAPIReference =\n | OpenAPIV2.ReferenceObject\n | OpenAPIV3.ReferenceObject\n | OpenAPIV3_1.ReferenceObject\n\n/**\n * Check if a value is an {@linkcode OpenAPIReference}.\n *\n * @param value The value to check.\n * @returns `true` if the value is a reference object.\n * @example isReferenceObject({ $ref: '#/components/schemas/MySchema' }) // true\n */\nexport function isReferenceObject<T extends OpenAPIReference>(value: unknown): value is T {\n return typeof value === 'object'\n && value !== null\n && '$ref' in value\n && typeof value.$ref === 'string'\n}\n","import type { StringJoin, StringReplace, WriteableDeep } from '@unshared/types'\nimport type { OpenAPIReference } from './isReferenceObject'\nimport { isReferenceObject } from './isReferenceObject'\n\n/**\n * Decode an OpenAPI reference path by replacing the encoded characters with\n * their original values. This function will replace `~0` with `~` and `~1`\n * with `/`.\n *\n * @example DecodeReference<'#/foo~1bar~0baz'> // '#/foo/bar~baz'\n */\nexport type OpenAPIReferenceDecoded<T extends string> =\n StringReplace<StringReplace<T, '~0', '~'>, '~1', '/'>\n\n/**\n * Extract the parts of a reference path as a tuple.\n *\n * @example OpenAPIV3ReferencePath<'#/paths/~1users~1{username}'> // ['paths', '/users/{username}']\n */\nexport type OpenAPIReferencePath<T extends string> =\n T extends `#/${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>]\n : T extends `#/${infer P}` ? [P]\n : T extends `${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>]\n : T extends `${infer P}` ? [P]\n : []\n\n/**\n * Resolve a type to the type it references. If the source is not a reference,\n * the source will be returned.\n *\n * @template T The type to resolve.\n * @returns The result type.\n * @example Resolved<{ $ref: '#/info' }, { info: { title: string } }> // { title: string }\n */\nexport type OpenAPIReferenceResolved<\n T extends OpenAPIReference,\n D extends object,\n> =\n D extends object\n ? T extends { $ref: infer R extends string }\n\n // --- Match last part of the reference.\n ? OpenAPIReferencePath<R> extends [infer P extends string]\n ? OpenAPIReferenceDecoded<P> extends keyof D\n ? D[OpenAPIReferenceDecoded<P>] extends object\n ? WriteableDeep<Omit<D[OpenAPIReferenceDecoded<P>], keyof T>>\n : never\n : never\n\n // --- Match middle part of the reference.\n : OpenAPIReferencePath<R> extends [infer P extends string, ...infer Rest extends string[]]\n ? OpenAPIReferenceDecoded<P> extends keyof D\n ? D[OpenAPIReferenceDecoded<P>] extends object\n ? OpenAPIReferenceResolved<{ $ref: StringJoin<Rest, '/'> }, D[OpenAPIReferenceDecoded<P>]>\n : never\n : never\n : never\n : never\n : never\n\n/**\n * Resolve an OpenAPI `ReferenceObject` to the component it references. If the\n * source is not a reference, the source will be returned.\n *\n * @private\n * @param reference The reference object to resolve.\n * @param document The OpenAPI document to resolve the reference from.\n * @returns The result component.\n * @example resolveReference({ $ref: '#/components/schemas/User' }, document)\n */\nexport function resolveReference<\n T extends OpenAPIReference,\n D extends object,\n>(reference: Readonly<T>, document: Readonly<D>): OpenAPIReferenceResolved<T, D> {\n\n // --- Return the source if it is not a reference.\n if (!isReferenceObject(reference))\n throw new TypeError('Expected value to be an OpenAPI reference object.')\n\n // --- Assert that the parameters are valid.\n if (typeof document !== 'object' || document === null)\n throw new TypeError('Expected OpenAPI specification to be an object.')\n\n // --- Resolve the component with it's reference path.\n const referenceParts = reference.$ref.replace(/^#\\//, '').split('/')\n let result = document\n for (const part of referenceParts) {\n if (result === undefined) break\n if (typeof result !== 'object' || result === null) break\n const key = part.replaceAll('~1', '/').replaceAll('~0', '~')\n // @ts-expect-error: assume the part is a key of the object.\n result = result[key] as unknown\n }\n\n // --- Throw an error if the component could not be result.\n if (result === undefined)\n throw new Error(`Could not resolve OpenAPI component: ${reference.$ref}`)\n\n // --- Return the result component.\n return result as OpenAPIReferenceResolved<T, D>\n}\n","import type { ObjectLike, Substract } from '@unshared/types'\nimport type { OpenAPIReference } from './isReferenceObject'\nimport type { OpenAPIReferenceResolved } from './resolveReference'\nimport { isReferenceObject } from './isReferenceObject'\nimport { resolveReference as resolveReference } from './resolveReference'\n\n/**\n * Resolve a type to the type it references. If the source is not a reference,\n * the source will be returned.\n *\n * @template T The type to resolve.\n * @returns The resolved type.\n * @example\n * type Resolved = OpenAPIResolved<{\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' }\n * }\n * }\n * }>\n */\nexport type OpenAPIResolved<T, D = T, N extends number = 8> =\n N extends 0 ? T\n : T extends OpenAPIReference\n ? D extends object ? OpenAPIResolved<OpenAPIReferenceResolved<T, D>, D, Substract<N, 1>> : never\n : T extends object ? { -readonly [K in keyof T]: OpenAPIResolved<T[K], D, N> } : T\n\n/**\n * Recursively resolve all references in an OpenAPI specification. This function\n * will return a `Proxy` object that will resolve references on the fly.\n *\n * @param value The OpenAPI specification.\n * @returns The resolved OpenAPI specification.\n * @example\n * const resolved = resolveReferences({\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' },\n * },\n * },\n * })\n */\nexport function resolveDocument<T extends object>(value: Readonly<T>): OpenAPIResolved<T>\nexport function resolveDocument<T extends object, D>(value: Readonly<T>, document: Readonly<D>): OpenAPIResolved<T, D>\nexport function resolveDocument(value: Readonly<ObjectLike>, document = value): unknown {\n return new Proxy(value, {\n get(target, property: string) {\n let value = target[property]\n\n // --- Abort if no document is provided.\n if (!document) return value\n\n // --- Resolve the reference if it is a reference object.\n if (isReferenceObject(value))\n value = resolveReference(value, document)\n\n // --- Recursively resolve references in objects.\n if (typeof value === 'object' && value !== null)\n return resolveDocument(value as ObjectLike, document)\n\n // --- Return the value as is.\n return value\n },\n })\n}\n"],"names":["value"],"mappings":";;AAUO,SAAS,YAAY,OAA6C;
|
1
|
+
{"version":3,"file":"openapi.cjs","sources":["../openapi/isOpenAPIV2.ts","../openapi/isOpenAPIV3_1.ts","../openapi/isReferenceObject.ts","../openapi/resolveReference.ts","../openapi/resolveDocument.ts"],"sourcesContent":["/* eslint-disable unicorn/filename-case */\nimport type { OpenAPIV2 } from 'openapi-types'\n\n/**\n * Check if the given document is an OpenAPI v2 specification.\n *\n * @param value The document to check.\n * @returns `true` if the document is an OpenAPI v2 specification, `false` otherwise.\n * @example isOpenAPIV2({ swagger: '2.0', info: { title: 'Test API', version: '1.0.0' } }) // => true\n */\nexport function isOpenAPIV2(value: unknown): value is OpenAPIV2.Document {\n return typeof value === 'object'\n && value !== null\n && 'swagger' in value\n && value.swagger === '2.0'\n}\n","/* eslint-disable unicorn/filename-case */\nimport type { OpenAPIV3_1 } from 'openapi-types'\n\n/**\n * Check if the given document is an OpenAPI v3.1 specification.\n *\n * @param value The document to check.\n * @returns `true` if the document is an OpenAPI v3.1 specification, `false` otherwise.\n * @example isOpenAPIV3_1({ openapi: '3.1.0', info: { title: 'Test API', version: '1.0.0' } }) // => true\n */\nexport function isOpenAPIV3_1(value: unknown): value is OpenAPIV3_1.Document {\n return typeof value === 'object'\n && value !== null\n && 'openapi' in value\n && value.openapi === '3.1.0'\n}\n","import type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport type OpenAPIReference =\n | OpenAPIV2.ReferenceObject\n | OpenAPIV3.ReferenceObject\n | OpenAPIV3_1.ReferenceObject\n\n/**\n * Check if a value is an {@linkcode OpenAPIReference}.\n *\n * @param value The value to check.\n * @returns `true` if the value is a reference object.\n * @example isReferenceObject({ $ref: '#/components/schemas/MySchema' }) // true\n */\nexport function isReferenceObject<T extends OpenAPIReference>(value: unknown): value is T {\n return typeof value === 'object'\n && value !== null\n && '$ref' in value\n && typeof value.$ref === 'string'\n}\n","import type { StringJoin, StringReplace, WriteableDeep } from '@unshared/types'\nimport type { OpenAPIReference } from './isReferenceObject'\nimport { isReferenceObject } from './isReferenceObject'\n\n/**\n * Decode an OpenAPI reference path by replacing the encoded characters with\n * their original values. This function will replace `~0` with `~` and `~1`\n * with `/`.\n *\n * @example DecodeReference<'#/foo~1bar~0baz'> // '#/foo/bar~baz'\n */\nexport type OpenAPIReferenceDecoded<T extends string> =\n StringReplace<StringReplace<T, '~0', '~'>, '~1', '/'>\n\n/**\n * Extract the parts of a reference path as a tuple.\n *\n * @example OpenAPIV3ReferencePath<'#/paths/~1users~1{username}'> // ['paths', '/users/{username}']\n */\nexport type OpenAPIReferencePath<T extends string> =\n T extends `#/${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>]\n : T extends `#/${infer P}` ? [P]\n : T extends `${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>]\n : T extends `${infer P}` ? [P]\n : []\n\n/**\n * Resolve a type to the type it references. If the source is not a reference,\n * the source will be returned.\n *\n * @template T The type to resolve.\n * @returns The result type.\n * @example Resolved<{ $ref: '#/info' }, { info: { title: string } }> // { title: string }\n */\nexport type OpenAPIReferenceResolved<\n T extends OpenAPIReference,\n D extends object,\n> =\n D extends object\n ? T extends { $ref: infer R extends string }\n\n // --- Match last part of the reference.\n ? OpenAPIReferencePath<R> extends [infer P extends string]\n ? OpenAPIReferenceDecoded<P> extends keyof D\n ? D[OpenAPIReferenceDecoded<P>] extends object\n ? WriteableDeep<Omit<D[OpenAPIReferenceDecoded<P>], keyof T>>\n : never\n : never\n\n // --- Match middle part of the reference.\n : OpenAPIReferencePath<R> extends [infer P extends string, ...infer Rest extends string[]]\n ? OpenAPIReferenceDecoded<P> extends keyof D\n ? D[OpenAPIReferenceDecoded<P>] extends object\n ? OpenAPIReferenceResolved<{ $ref: StringJoin<Rest, '/'> }, D[OpenAPIReferenceDecoded<P>]>\n : never\n : never\n : never\n : never\n : never\n\n/**\n * Resolve an OpenAPI `ReferenceObject` to the component it references. If the\n * source is not a reference, the source will be returned.\n *\n * @private\n * @param reference The reference object to resolve.\n * @param document The OpenAPI document to resolve the reference from.\n * @returns The result component.\n * @example resolveReference({ $ref: '#/components/schemas/User' }, document)\n */\nexport function resolveReference<\n T extends OpenAPIReference,\n D extends object,\n>(reference: Readonly<T>, document: Readonly<D>): OpenAPIReferenceResolved<T, D> {\n\n // --- Return the source if it is not a reference.\n if (!isReferenceObject(reference))\n throw new TypeError('Expected value to be an OpenAPI reference object.')\n\n // --- Assert that the parameters are valid.\n if (typeof document !== 'object' || document === null)\n throw new TypeError('Expected OpenAPI specification to be an object.')\n\n // --- Resolve the component with it's reference path.\n const referenceParts = reference.$ref.replace(/^#\\//, '').split('/')\n let result = document\n for (const part of referenceParts) {\n if (result === undefined) break\n if (typeof result !== 'object' || result === null) break\n const key = part.replaceAll('~1', '/').replaceAll('~0', '~')\n // @ts-expect-error: assume the part is a key of the object.\n result = result[key] as unknown\n }\n\n // --- Throw an error if the component could not be result.\n if (result === undefined)\n throw new Error(`Could not resolve OpenAPI component: ${reference.$ref}`)\n\n // --- Return the result component.\n return result as OpenAPIReferenceResolved<T, D>\n}\n","import type { ObjectLike, Substract } from '@unshared/types'\nimport type { OpenAPIReference } from './isReferenceObject'\nimport type { OpenAPIReferenceResolved } from './resolveReference'\nimport { isReferenceObject } from './isReferenceObject'\nimport { resolveReference as resolveReference } from './resolveReference'\n\n/**\n * Resolve a type to the type it references. If the source is not a reference,\n * the source will be returned.\n *\n * @template T The type to resolve.\n * @returns The resolved type.\n * @example\n * type Resolved = OpenAPIResolved<{\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' }\n * }\n * }\n * }>\n */\nexport type OpenAPIResolved<T, D = T, N extends number = 8> =\n N extends 0 ? T\n : T extends OpenAPIReference\n ? D extends object ? OpenAPIResolved<OpenAPIReferenceResolved<T, D>, D, Substract<N, 1>> : never\n : T extends object ? { -readonly [K in keyof T]: OpenAPIResolved<T[K], D, N> } : T\n\n/**\n * Recursively resolve all references in an OpenAPI specification. This function\n * will return a `Proxy` object that will resolve references on the fly.\n *\n * @param value The OpenAPI specification.\n * @returns The resolved OpenAPI specification.\n * @example\n * const resolved = resolveReferences({\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' },\n * },\n * },\n * })\n */\nexport function resolveDocument<T extends object>(value: Readonly<T>): OpenAPIResolved<T>\n\n/**\n * Recursively resolve all references in an OpenAPI specification. This function\n * will return a `Proxy` object that will resolve references on the fly.\n *\n * @param value The OpenAPI specification.\n * @param document The OpenAPI document to resolve references against.\n * @returns The resolved OpenAPI specification.\n * @example\n * const resolved = resolveReferences({\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' },\n * },\n * },\n * })\n */\nexport function resolveDocument<T extends object, D>(value: Readonly<T>, document: Readonly<D>): OpenAPIResolved<T, D>\nexport function resolveDocument(value: Readonly<ObjectLike>, document = value): unknown {\n return new Proxy(value, {\n get(target, property: string) {\n let value = target[property]\n\n // --- Abort if no document is provided.\n if (!document) return value\n\n // --- Resolve the reference if it is a reference object.\n if (isReferenceObject(value))\n value = resolveReference(value, document)\n\n // --- Recursively resolve references in objects.\n if (typeof value === 'object' && value !== null)\n return resolveDocument(value as ObjectLike, document)\n\n // --- Return the value as is.\n return value\n },\n })\n}\n"],"names":["value"],"mappings":";;AAUO,SAAS,YAAY,OAA6C;AACvE,SAAO,OAAO,SAAU,YACnB,UAAU,QACV,aAAa,SACb,MAAM,YAAY;AACzB;ACLO,SAAS,cAAc,OAA+C;AAC3E,SAAO,OAAO,SAAU,YACnB,UAAU,QACV,aAAa,SACb,MAAM,YAAY;AACzB;ACDO,SAAS,kBAA8C,OAA4B;AACxF,SAAO,OAAO,SAAU,YACnB,UAAU,QACV,UAAU,SACV,OAAO,MAAM,QAAS;AAC7B;ACmDO,SAAS,iBAGd,WAAwB,UAAuD;AAG/E,MAAI,CAAC,kBAAkB,SAAS;AAC9B,UAAM,IAAI,UAAU,mDAAmD;AAGzE,MAAI,OAAO,YAAa,YAAY,aAAa;AAC/C,UAAM,IAAI,UAAU,iDAAiD;AAGvE,QAAM,iBAAiB,UAAU,KAAK,QAAQ,QAAQ,EAAE,EAAE,MAAM,GAAG;AACnE,MAAI,SAAS;AACb,aAAW,QAAQ,gBAAgB;AAEjC,QADI,WAAW,UACX,OAAO,UAAW,YAAY,WAAW,KAAM;AACnD,UAAM,MAAM,KAAK,WAAW,MAAM,GAAG,EAAE,WAAW,MAAM,GAAG;AAE3D,aAAS,OAAO,GAAG;AAAA,EACrB;AAGA,MAAI,WAAW;AACb,UAAM,IAAI,MAAM,wCAAwC,UAAU,IAAI,EAAE;AAG1E,SAAO;AACT;ACpCO,SAAS,gBAAgB,OAA6B,WAAW,OAAgB;AACtF,SAAO,IAAI,MAAM,OAAO;AAAA,IACtB,IAAI,QAAQ,UAAkB;AAC5B,UAAIA,SAAQ,OAAO,QAAQ;AAG3B,aAAK,YAGD,kBAAkBA,MAAK,MACzBA,SAAQ,iBAAiBA,QAAO,QAAQ,IAGtC,OAAOA,UAAU,YAAYA,WAAU,OAClC,gBAAgBA,QAAqB,QAAQ,IAG/CA,UAXeA;AAAAA,IAYxB;AAAA,EAAA,CACD;AACH;;;;;;;;;;"}
|
package/dist/openapi.d.ts
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
-
export { O as OpenAPILike, a as OpenAPIOptionsMap, h as OpenAPIV2, j as OpenAPIV2Like, b as OpenAPIV3, k as OpenAPIV3Like, i as Operation, d as OperationById, n as OperationByRoute, c as OperationId, e as OperationOptions, l as OperationResponse, f as OperationResult, m as OperationRoute, S as ServerUrl, g as getServerUrl, r as resolveOperation } from './chunks/
|
1
|
+
export { O as OpenAPILike, a as OpenAPIOptionsMap, h as OpenAPIV2, j as OpenAPIV2Like, b as OpenAPIV3, k as OpenAPIV3Like, i as Operation, d as OperationById, n as OperationByRoute, c as OperationId, e as OperationOptions, l as OperationResponse, f as OperationResult, m as OperationRoute, S as ServerUrl, g as getServerUrl, r as resolveOperation } from './chunks/DKPTfBgy.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/
|
4
|
+
import './chunks/CNqEW-S3.js';
|
5
|
+
import '@unshared/functions/awaitable';
|
5
6
|
import './HttpHeaders.js';
|
6
7
|
import './HttpMethods.js';
|
7
8
|
|
@@ -117,6 +118,23 @@ type OpenAPIResolved<T, D = T, N extends number = 8> = N extends 0 ? T : T exten
|
|
117
118
|
* })
|
118
119
|
*/
|
119
120
|
declare function resolveDocument<T extends object>(value: Readonly<T>): OpenAPIResolved<T>;
|
121
|
+
/**
|
122
|
+
* Recursively resolve all references in an OpenAPI specification. This function
|
123
|
+
* will return a `Proxy` object that will resolve references on the fly.
|
124
|
+
*
|
125
|
+
* @param value The OpenAPI specification.
|
126
|
+
* @param document The OpenAPI document to resolve references against.
|
127
|
+
* @returns The resolved OpenAPI specification.
|
128
|
+
* @example
|
129
|
+
* const resolved = resolveReferences({
|
130
|
+
* ...
|
131
|
+
* paths: {
|
132
|
+
* '/users': {
|
133
|
+
* get: { $ref: '#/components/routes/getUsers' },
|
134
|
+
* },
|
135
|
+
* },
|
136
|
+
* })
|
137
|
+
*/
|
120
138
|
declare function resolveDocument<T extends object, D>(value: Readonly<T>, document: Readonly<D>): OpenAPIResolved<T, D>;
|
121
139
|
|
122
140
|
interface TokenOptions {
|
package/dist/openapi.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"openapi.js","sources":["../openapi/isOpenAPIV2.ts","../openapi/isOpenAPIV3_1.ts","../openapi/isReferenceObject.ts","../openapi/resolveReference.ts","../openapi/resolveDocument.ts"],"sourcesContent":["/* eslint-disable unicorn/filename-case */\nimport type { OpenAPIV2 } from 'openapi-types'\n\n/**\n * Check if the given document is an OpenAPI v2 specification.\n *\n * @param value The document to check.\n * @returns `true` if the document is an OpenAPI v2 specification, `false` otherwise.\n * @example isOpenAPIV2({ swagger: '2.0', info: { title: 'Test API', version: '1.0.0' } }) // => true\n */\nexport function isOpenAPIV2(value: unknown): value is OpenAPIV2.Document {\n return typeof value === 'object'\n && value !== null\n && 'swagger' in value\n && value.swagger === '2.0'\n}\n","/* eslint-disable unicorn/filename-case */\nimport type { OpenAPIV3_1 } from 'openapi-types'\n\n/**\n * Check if the given document is an OpenAPI v3.1 specification.\n *\n * @param value The document to check.\n * @returns `true` if the document is an OpenAPI v3.1 specification, `false` otherwise.\n * @example isOpenAPIV3_1({ openapi: '3.1.0', info: { title: 'Test API', version: '1.0.0' } }) // => true\n */\nexport function isOpenAPIV3_1(value: unknown): value is OpenAPIV3_1.Document {\n return typeof value === 'object'\n && value !== null\n && 'openapi' in value\n && value.openapi === '3.1.0'\n}\n","import type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport type OpenAPIReference =\n | OpenAPIV2.ReferenceObject\n | OpenAPIV3.ReferenceObject\n | OpenAPIV3_1.ReferenceObject\n\n/**\n * Check if a value is an {@linkcode OpenAPIReference}.\n *\n * @param value The value to check.\n * @returns `true` if the value is a reference object.\n * @example isReferenceObject({ $ref: '#/components/schemas/MySchema' }) // true\n */\nexport function isReferenceObject<T extends OpenAPIReference>(value: unknown): value is T {\n return typeof value === 'object'\n && value !== null\n && '$ref' in value\n && typeof value.$ref === 'string'\n}\n","import type { StringJoin, StringReplace, WriteableDeep } from '@unshared/types'\nimport type { OpenAPIReference } from './isReferenceObject'\nimport { isReferenceObject } from './isReferenceObject'\n\n/**\n * Decode an OpenAPI reference path by replacing the encoded characters with\n * their original values. This function will replace `~0` with `~` and `~1`\n * with `/`.\n *\n * @example DecodeReference<'#/foo~1bar~0baz'> // '#/foo/bar~baz'\n */\nexport type OpenAPIReferenceDecoded<T extends string> =\n StringReplace<StringReplace<T, '~0', '~'>, '~1', '/'>\n\n/**\n * Extract the parts of a reference path as a tuple.\n *\n * @example OpenAPIV3ReferencePath<'#/paths/~1users~1{username}'> // ['paths', '/users/{username}']\n */\nexport type OpenAPIReferencePath<T extends string> =\n T extends `#/${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>]\n : T extends `#/${infer P}` ? [P]\n : T extends `${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>]\n : T extends `${infer P}` ? [P]\n : []\n\n/**\n * Resolve a type to the type it references. If the source is not a reference,\n * the source will be returned.\n *\n * @template T The type to resolve.\n * @returns The result type.\n * @example Resolved<{ $ref: '#/info' }, { info: { title: string } }> // { title: string }\n */\nexport type OpenAPIReferenceResolved<\n T extends OpenAPIReference,\n D extends object,\n> =\n D extends object\n ? T extends { $ref: infer R extends string }\n\n // --- Match last part of the reference.\n ? OpenAPIReferencePath<R> extends [infer P extends string]\n ? OpenAPIReferenceDecoded<P> extends keyof D\n ? D[OpenAPIReferenceDecoded<P>] extends object\n ? WriteableDeep<Omit<D[OpenAPIReferenceDecoded<P>], keyof T>>\n : never\n : never\n\n // --- Match middle part of the reference.\n : OpenAPIReferencePath<R> extends [infer P extends string, ...infer Rest extends string[]]\n ? OpenAPIReferenceDecoded<P> extends keyof D\n ? D[OpenAPIReferenceDecoded<P>] extends object\n ? OpenAPIReferenceResolved<{ $ref: StringJoin<Rest, '/'> }, D[OpenAPIReferenceDecoded<P>]>\n : never\n : never\n : never\n : never\n : never\n\n/**\n * Resolve an OpenAPI `ReferenceObject` to the component it references. If the\n * source is not a reference, the source will be returned.\n *\n * @private\n * @param reference The reference object to resolve.\n * @param document The OpenAPI document to resolve the reference from.\n * @returns The result component.\n * @example resolveReference({ $ref: '#/components/schemas/User' }, document)\n */\nexport function resolveReference<\n T extends OpenAPIReference,\n D extends object,\n>(reference: Readonly<T>, document: Readonly<D>): OpenAPIReferenceResolved<T, D> {\n\n // --- Return the source if it is not a reference.\n if (!isReferenceObject(reference))\n throw new TypeError('Expected value to be an OpenAPI reference object.')\n\n // --- Assert that the parameters are valid.\n if (typeof document !== 'object' || document === null)\n throw new TypeError('Expected OpenAPI specification to be an object.')\n\n // --- Resolve the component with it's reference path.\n const referenceParts = reference.$ref.replace(/^#\\//, '').split('/')\n let result = document\n for (const part of referenceParts) {\n if (result === undefined) break\n if (typeof result !== 'object' || result === null) break\n const key = part.replaceAll('~1', '/').replaceAll('~0', '~')\n // @ts-expect-error: assume the part is a key of the object.\n result = result[key] as unknown\n }\n\n // --- Throw an error if the component could not be result.\n if (result === undefined)\n throw new Error(`Could not resolve OpenAPI component: ${reference.$ref}`)\n\n // --- Return the result component.\n return result as OpenAPIReferenceResolved<T, D>\n}\n","import type { ObjectLike, Substract } from '@unshared/types'\nimport type { OpenAPIReference } from './isReferenceObject'\nimport type { OpenAPIReferenceResolved } from './resolveReference'\nimport { isReferenceObject } from './isReferenceObject'\nimport { resolveReference as resolveReference } from './resolveReference'\n\n/**\n * Resolve a type to the type it references. If the source is not a reference,\n * the source will be returned.\n *\n * @template T The type to resolve.\n * @returns The resolved type.\n * @example\n * type Resolved = OpenAPIResolved<{\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' }\n * }\n * }\n * }>\n */\nexport type OpenAPIResolved<T, D = T, N extends number = 8> =\n N extends 0 ? T\n : T extends OpenAPIReference\n ? D extends object ? OpenAPIResolved<OpenAPIReferenceResolved<T, D>, D, Substract<N, 1>> : never\n : T extends object ? { -readonly [K in keyof T]: OpenAPIResolved<T[K], D, N> } : T\n\n/**\n * Recursively resolve all references in an OpenAPI specification. This function\n * will return a `Proxy` object that will resolve references on the fly.\n *\n * @param value The OpenAPI specification.\n * @returns The resolved OpenAPI specification.\n * @example\n * const resolved = resolveReferences({\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' },\n * },\n * },\n * })\n */\nexport function resolveDocument<T extends object>(value: Readonly<T>): OpenAPIResolved<T>\nexport function resolveDocument<T extends object, D>(value: Readonly<T>, document: Readonly<D>): OpenAPIResolved<T, D>\nexport function resolveDocument(value: Readonly<ObjectLike>, document = value): unknown {\n return new Proxy(value, {\n get(target, property: string) {\n let value = target[property]\n\n // --- Abort if no document is provided.\n if (!document) return value\n\n // --- Resolve the reference if it is a reference object.\n if (isReferenceObject(value))\n value = resolveReference(value, document)\n\n // --- Recursively resolve references in objects.\n if (typeof value === 'object' && value !== null)\n return resolveDocument(value as ObjectLike, document)\n\n // --- Return the value as is.\n return value\n },\n })\n}\n"],"names":["value"],"mappings":";AAUO,SAAS,YAAY,OAA6C;
|
1
|
+
{"version":3,"file":"openapi.js","sources":["../openapi/isOpenAPIV2.ts","../openapi/isOpenAPIV3_1.ts","../openapi/isReferenceObject.ts","../openapi/resolveReference.ts","../openapi/resolveDocument.ts"],"sourcesContent":["/* eslint-disable unicorn/filename-case */\nimport type { OpenAPIV2 } from 'openapi-types'\n\n/**\n * Check if the given document is an OpenAPI v2 specification.\n *\n * @param value The document to check.\n * @returns `true` if the document is an OpenAPI v2 specification, `false` otherwise.\n * @example isOpenAPIV2({ swagger: '2.0', info: { title: 'Test API', version: '1.0.0' } }) // => true\n */\nexport function isOpenAPIV2(value: unknown): value is OpenAPIV2.Document {\n return typeof value === 'object'\n && value !== null\n && 'swagger' in value\n && value.swagger === '2.0'\n}\n","/* eslint-disable unicorn/filename-case */\nimport type { OpenAPIV3_1 } from 'openapi-types'\n\n/**\n * Check if the given document is an OpenAPI v3.1 specification.\n *\n * @param value The document to check.\n * @returns `true` if the document is an OpenAPI v3.1 specification, `false` otherwise.\n * @example isOpenAPIV3_1({ openapi: '3.1.0', info: { title: 'Test API', version: '1.0.0' } }) // => true\n */\nexport function isOpenAPIV3_1(value: unknown): value is OpenAPIV3_1.Document {\n return typeof value === 'object'\n && value !== null\n && 'openapi' in value\n && value.openapi === '3.1.0'\n}\n","import type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport type OpenAPIReference =\n | OpenAPIV2.ReferenceObject\n | OpenAPIV3.ReferenceObject\n | OpenAPIV3_1.ReferenceObject\n\n/**\n * Check if a value is an {@linkcode OpenAPIReference}.\n *\n * @param value The value to check.\n * @returns `true` if the value is a reference object.\n * @example isReferenceObject({ $ref: '#/components/schemas/MySchema' }) // true\n */\nexport function isReferenceObject<T extends OpenAPIReference>(value: unknown): value is T {\n return typeof value === 'object'\n && value !== null\n && '$ref' in value\n && typeof value.$ref === 'string'\n}\n","import type { StringJoin, StringReplace, WriteableDeep } from '@unshared/types'\nimport type { OpenAPIReference } from './isReferenceObject'\nimport { isReferenceObject } from './isReferenceObject'\n\n/**\n * Decode an OpenAPI reference path by replacing the encoded characters with\n * their original values. This function will replace `~0` with `~` and `~1`\n * with `/`.\n *\n * @example DecodeReference<'#/foo~1bar~0baz'> // '#/foo/bar~baz'\n */\nexport type OpenAPIReferenceDecoded<T extends string> =\n StringReplace<StringReplace<T, '~0', '~'>, '~1', '/'>\n\n/**\n * Extract the parts of a reference path as a tuple.\n *\n * @example OpenAPIV3ReferencePath<'#/paths/~1users~1{username}'> // ['paths', '/users/{username}']\n */\nexport type OpenAPIReferencePath<T extends string> =\n T extends `#/${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>]\n : T extends `#/${infer P}` ? [P]\n : T extends `${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>]\n : T extends `${infer P}` ? [P]\n : []\n\n/**\n * Resolve a type to the type it references. If the source is not a reference,\n * the source will be returned.\n *\n * @template T The type to resolve.\n * @returns The result type.\n * @example Resolved<{ $ref: '#/info' }, { info: { title: string } }> // { title: string }\n */\nexport type OpenAPIReferenceResolved<\n T extends OpenAPIReference,\n D extends object,\n> =\n D extends object\n ? T extends { $ref: infer R extends string }\n\n // --- Match last part of the reference.\n ? OpenAPIReferencePath<R> extends [infer P extends string]\n ? OpenAPIReferenceDecoded<P> extends keyof D\n ? D[OpenAPIReferenceDecoded<P>] extends object\n ? WriteableDeep<Omit<D[OpenAPIReferenceDecoded<P>], keyof T>>\n : never\n : never\n\n // --- Match middle part of the reference.\n : OpenAPIReferencePath<R> extends [infer P extends string, ...infer Rest extends string[]]\n ? OpenAPIReferenceDecoded<P> extends keyof D\n ? D[OpenAPIReferenceDecoded<P>] extends object\n ? OpenAPIReferenceResolved<{ $ref: StringJoin<Rest, '/'> }, D[OpenAPIReferenceDecoded<P>]>\n : never\n : never\n : never\n : never\n : never\n\n/**\n * Resolve an OpenAPI `ReferenceObject` to the component it references. If the\n * source is not a reference, the source will be returned.\n *\n * @private\n * @param reference The reference object to resolve.\n * @param document The OpenAPI document to resolve the reference from.\n * @returns The result component.\n * @example resolveReference({ $ref: '#/components/schemas/User' }, document)\n */\nexport function resolveReference<\n T extends OpenAPIReference,\n D extends object,\n>(reference: Readonly<T>, document: Readonly<D>): OpenAPIReferenceResolved<T, D> {\n\n // --- Return the source if it is not a reference.\n if (!isReferenceObject(reference))\n throw new TypeError('Expected value to be an OpenAPI reference object.')\n\n // --- Assert that the parameters are valid.\n if (typeof document !== 'object' || document === null)\n throw new TypeError('Expected OpenAPI specification to be an object.')\n\n // --- Resolve the component with it's reference path.\n const referenceParts = reference.$ref.replace(/^#\\//, '').split('/')\n let result = document\n for (const part of referenceParts) {\n if (result === undefined) break\n if (typeof result !== 'object' || result === null) break\n const key = part.replaceAll('~1', '/').replaceAll('~0', '~')\n // @ts-expect-error: assume the part is a key of the object.\n result = result[key] as unknown\n }\n\n // --- Throw an error if the component could not be result.\n if (result === undefined)\n throw new Error(`Could not resolve OpenAPI component: ${reference.$ref}`)\n\n // --- Return the result component.\n return result as OpenAPIReferenceResolved<T, D>\n}\n","import type { ObjectLike, Substract } from '@unshared/types'\nimport type { OpenAPIReference } from './isReferenceObject'\nimport type { OpenAPIReferenceResolved } from './resolveReference'\nimport { isReferenceObject } from './isReferenceObject'\nimport { resolveReference as resolveReference } from './resolveReference'\n\n/**\n * Resolve a type to the type it references. If the source is not a reference,\n * the source will be returned.\n *\n * @template T The type to resolve.\n * @returns The resolved type.\n * @example\n * type Resolved = OpenAPIResolved<{\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' }\n * }\n * }\n * }>\n */\nexport type OpenAPIResolved<T, D = T, N extends number = 8> =\n N extends 0 ? T\n : T extends OpenAPIReference\n ? D extends object ? OpenAPIResolved<OpenAPIReferenceResolved<T, D>, D, Substract<N, 1>> : never\n : T extends object ? { -readonly [K in keyof T]: OpenAPIResolved<T[K], D, N> } : T\n\n/**\n * Recursively resolve all references in an OpenAPI specification. This function\n * will return a `Proxy` object that will resolve references on the fly.\n *\n * @param value The OpenAPI specification.\n * @returns The resolved OpenAPI specification.\n * @example\n * const resolved = resolveReferences({\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' },\n * },\n * },\n * })\n */\nexport function resolveDocument<T extends object>(value: Readonly<T>): OpenAPIResolved<T>\n\n/**\n * Recursively resolve all references in an OpenAPI specification. This function\n * will return a `Proxy` object that will resolve references on the fly.\n *\n * @param value The OpenAPI specification.\n * @param document The OpenAPI document to resolve references against.\n * @returns The resolved OpenAPI specification.\n * @example\n * const resolved = resolveReferences({\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' },\n * },\n * },\n * })\n */\nexport function resolveDocument<T extends object, D>(value: Readonly<T>, document: Readonly<D>): OpenAPIResolved<T, D>\nexport function resolveDocument(value: Readonly<ObjectLike>, document = value): unknown {\n return new Proxy(value, {\n get(target, property: string) {\n let value = target[property]\n\n // --- Abort if no document is provided.\n if (!document) return value\n\n // --- Resolve the reference if it is a reference object.\n if (isReferenceObject(value))\n value = resolveReference(value, document)\n\n // --- Recursively resolve references in objects.\n if (typeof value === 'object' && value !== null)\n return resolveDocument(value as ObjectLike, document)\n\n // --- Return the value as is.\n return value\n },\n })\n}\n"],"names":["value"],"mappings":";AAUO,SAAS,YAAY,OAA6C;AACvE,SAAO,OAAO,SAAU,YACnB,UAAU,QACV,aAAa,SACb,MAAM,YAAY;AACzB;ACLO,SAAS,cAAc,OAA+C;AAC3E,SAAO,OAAO,SAAU,YACnB,UAAU,QACV,aAAa,SACb,MAAM,YAAY;AACzB;ACDO,SAAS,kBAA8C,OAA4B;AACxF,SAAO,OAAO,SAAU,YACnB,UAAU,QACV,UAAU,SACV,OAAO,MAAM,QAAS;AAC7B;ACmDO,SAAS,iBAGd,WAAwB,UAAuD;AAG/E,MAAI,CAAC,kBAAkB,SAAS;AAC9B,UAAM,IAAI,UAAU,mDAAmD;AAGzE,MAAI,OAAO,YAAa,YAAY,aAAa;AAC/C,UAAM,IAAI,UAAU,iDAAiD;AAGvE,QAAM,iBAAiB,UAAU,KAAK,QAAQ,QAAQ,EAAE,EAAE,MAAM,GAAG;AACnE,MAAI,SAAS;AACb,aAAW,QAAQ,gBAAgB;AAEjC,QADI,WAAW,UACX,OAAO,UAAW,YAAY,WAAW,KAAM;AACnD,UAAM,MAAM,KAAK,WAAW,MAAM,GAAG,EAAE,WAAW,MAAM,GAAG;AAE3D,aAAS,OAAO,GAAG;AAAA,EACrB;AAGA,MAAI,WAAW;AACb,UAAM,IAAI,MAAM,wCAAwC,UAAU,IAAI,EAAE;AAG1E,SAAO;AACT;ACpCO,SAAS,gBAAgB,OAA6B,WAAW,OAAgB;AACtF,SAAO,IAAI,MAAM,OAAO;AAAA,IACtB,IAAI,QAAQ,UAAkB;AAC5B,UAAIA,SAAQ,OAAO,QAAQ;AAG3B,aAAK,YAGD,kBAAkBA,MAAK,MACzBA,SAAQ,iBAAiBA,QAAO,QAAQ,IAGtC,OAAOA,UAAU,YAAYA,WAAU,OAClC,gBAAgBA,QAAqB,QAAQ,IAG/CA,UAXeA;AAAAA,IAYxB;AAAA,EAAA,CACD;AACH;"}
|
package/dist/utils.cjs
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
"use strict";
|
2
|
-
var request = require("./chunks/
|
2
|
+
var request = require("./chunks/CNNxr5Ws.cjs"), handleResponse = require("./chunks/-InYnohy.cjs"), parseRequestQuery = require("./chunks/BDxlAULu.cjs");
|
3
3
|
require("@unshared/functions/awaitable");
|
4
4
|
function getCookie(headers, key) {
|
5
5
|
const cookie = handleResponse.getCookies(headers);
|
package/dist/utils.cjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.cjs","sources":["../utils/getCookie.ts"],"sourcesContent":["import { getCookies } from './getCookies'\n\n/**\n * Get a cookie value from the `HeadersInit` object.\n *\n * @param headers The headers to get the cookie value from.\n * @param key The key of the cookie to get.\n * @returns The value of the cookie.\n * @example\n * const headers = new Headers({ Cookie: 'key1=value1; key2=value2' })\n * const value = getCookie(headers, 'key1')\n */\nexport function getCookie(headers: HeadersInit, key: string): string | undefined {\n const cookie = getCookies(headers)\n if (!cookie) return\n return cookie[key]\n}\n"],"names":["getCookies"],"mappings":";;;
|
1
|
+
{"version":3,"file":"utils.cjs","sources":["../utils/getCookie.ts"],"sourcesContent":["import { getCookies } from './getCookies'\n\n/**\n * Get a cookie value from the `HeadersInit` object.\n *\n * @param headers The headers to get the cookie value from.\n * @param key The key of the cookie to get.\n * @returns The value of the cookie.\n * @example\n * const headers = new Headers({ Cookie: 'key1=value1; key2=value2' })\n * const value = getCookie(headers, 'key1')\n */\nexport function getCookie(headers: HeadersInit, key: string): string | undefined {\n const cookie = getCookies(headers)\n if (!cookie) return\n return cookie[key]\n}\n"],"names":["getCookies"],"mappings":";;;AAYO,SAAS,UAAU,SAAsB,KAAiC;AAC/E,QAAM,SAASA,eAAAA,WAAW,OAAO;AACjC,MAAK;AACL,WAAO,OAAO,GAAG;AACnB;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/utils.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import { F as FetchOptions, R as RequestOptions, b as RequestContext } from './chunks/
|
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/
|
1
|
+
import { F as FetchOptions, R as RequestOptions, b as RequestContext } from './chunks/CNqEW-S3.js';
|
2
|
+
export { c as FetchHeaders, a as FetchMethod, d as RequestOptionsOnData, e as SearchArrayFormat, S as SearchParamsObject, T as ToSearchParamsOptions, p as parseRequest, r as request, t as toSearchParams } from './chunks/CNqEW-S3.js';
|
3
3
|
import { Awaitable } from '@unshared/functions/awaitable';
|
4
4
|
import { ObjectLike } from '@unshared/types';
|
5
5
|
import './HttpHeaders.js';
|
@@ -75,11 +75,11 @@ declare function handleResponse(response: Response, options?: RequestOptions): P
|
|
75
75
|
declare function handleResponseStreamJson(response: Response, options: RequestOptions): Awaitable<AsyncIterable<unknown>, unknown[]>;
|
76
76
|
|
77
77
|
/** SSE event data structure */
|
78
|
-
interface SseEvent {
|
78
|
+
interface SseEvent<T = string> {
|
79
79
|
/** The event type */
|
80
80
|
event?: string;
|
81
81
|
/** The event data */
|
82
|
-
data:
|
82
|
+
data: T;
|
83
83
|
/** The event ID */
|
84
84
|
id?: string;
|
85
85
|
/** The retry timeout in milliseconds */
|
@@ -94,7 +94,7 @@ interface SseEvent {
|
|
94
94
|
* @param options The options to pass to the request.
|
95
95
|
* @returns An awaitable iterator that yields the parsed SSE events.
|
96
96
|
*/
|
97
|
-
declare function handleResponseStreamSse(response: Response, options: RequestOptions): Awaitable<AsyncIterable<SseEvent
|
97
|
+
declare function handleResponseStreamSse<T>(response: Response, options: RequestOptions): Awaitable<AsyncIterable<SseEvent<T>>, Array<SseEvent<T>>>;
|
98
98
|
|
99
99
|
/**
|
100
100
|
* A type that represents a FormData-like object, which is a plain object with
|
package/dist/utils.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
import { f, r } from "./chunks/
|
2
|
-
import { g as getCookies } from "./chunks/
|
3
|
-
import { a, h, b, c, i, d, p, e, f as f2, j, k, l, s, m, t } from "./chunks/
|
1
|
+
import { f, r } from "./chunks/ClHrsaTt.js";
|
2
|
+
import { g as getCookies } from "./chunks/DZ2MwV7J.js";
|
3
|
+
import { a, h, b, c, i, d, p, e, f as f2, j, k, l, s, m, t } from "./chunks/DZ2MwV7J.js";
|
4
4
|
import { p as p2, a as a2, t as t2 } from "./chunks/B6pUErTM.js";
|
5
5
|
import "@unshared/functions/awaitable";
|
6
6
|
function getCookie(headers, key) {
|
package/dist/utils.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.js","sources":["../utils/getCookie.ts"],"sourcesContent":["import { getCookies } from './getCookies'\n\n/**\n * Get a cookie value from the `HeadersInit` object.\n *\n * @param headers The headers to get the cookie value from.\n * @param key The key of the cookie to get.\n * @returns The value of the cookie.\n * @example\n * const headers = new Headers({ Cookie: 'key1=value1; key2=value2' })\n * const value = getCookie(headers, 'key1')\n */\nexport function getCookie(headers: HeadersInit, key: string): string | undefined {\n const cookie = getCookies(headers)\n if (!cookie) return\n return cookie[key]\n}\n"],"names":[],"mappings":";;;;;
|
1
|
+
{"version":3,"file":"utils.js","sources":["../utils/getCookie.ts"],"sourcesContent":["import { getCookies } from './getCookies'\n\n/**\n * Get a cookie value from the `HeadersInit` object.\n *\n * @param headers The headers to get the cookie value from.\n * @param key The key of the cookie to get.\n * @returns The value of the cookie.\n * @example\n * const headers = new Headers({ Cookie: 'key1=value1; key2=value2' })\n * const value = getCookie(headers, 'key1')\n */\nexport function getCookie(headers: HeadersInit, key: string): string | undefined {\n const cookie = getCookies(headers)\n if (!cookie) return\n return cookie[key]\n}\n"],"names":[],"mappings":";;;;;AAYO,SAAS,UAAU,SAAsB,KAAiC;AAC/E,QAAM,SAAS,WAAW,OAAO;AACjC,MAAK;AACL,WAAO,OAAO,GAAG;AACnB;"}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@unshared/client",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.7.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.
|
72
|
-
"@unshared/types": "0.
|
71
|
+
"@unshared/functions": "0.7.0",
|
72
|
+
"@unshared/types": "0.7.0",
|
73
73
|
"openapi-types": "12.1.3"
|
74
74
|
},
|
75
75
|
"devDependencies": {
|
76
|
-
"@unshared/scripts": "0.
|
76
|
+
"@unshared/scripts": "0.7.0"
|
77
77
|
},
|
78
78
|
"scripts": {
|
79
79
|
"build:httpMethods": "tsx ./scripts/buildHttpMethods.ts",
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"CQUndCW0.cjs","sources":["../../utils/fetch.ts","../../utils/request.ts"],"sourcesContent":["import type { FetchOptions } from './parseRequest'\nimport { parseRequest } from './parseRequest'\n\n/**\n * Fetch a route with the provided options. This function will parse the route and\n * options to create a `Request` object and return the response from the server.\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 * @example fetch('GET /users', { query: { limit: 10 } })\n */\nexport async function fetch(route: string, options?: FetchOptions): Promise<Response>\nexport async function fetch(route: string, options: FetchOptions = {}): Promise<Response> {\n const { url, init } = parseRequest(route, options)\n if (!url) throw new Error('Could not parse request URL')\n return await globalThis.fetch(url, init)\n}\n","import type { ObjectLike } from '@unshared/types'\nimport type { FetchMethod, FetchOptions } from './parseRequest'\nimport { fetch } from './fetch'\nimport { handleResponse } from './handleResponse'\n\nexport interface RequestOptions<\n Method extends FetchMethod = FetchMethod,\n BaseUrl extends string = string,\n Parameters extends ObjectLike = ObjectLike,\n Query extends ObjectLike = ObjectLike,\n Body = unknown,\n Headers extends ObjectLike = ObjectLike,\n Data = any,\n Response = globalThis.Response,\n> extends\n FetchOptions<Method, BaseUrl, Parameters, Query, Body, Headers> {\n\n /**\n * The callback that is called when an error occurs during the request.\n */\n onError?: (error: Error) => any\n\n /**\n * The callback that is called when data is received from the request. This callback\n * will be called for each chunk of data that is received from the request.\n */\n onData?: (data: Data) => any\n\n /**\n * The callback that is called when the request is successful. This callback will be\n * called after the request is complete and all data has been received.\n */\n onSuccess?: (response: Response) => any\n\n /**\n * The callback that is called when the status code is not OK. This callback will be called\n * after the request is complete and before the data is consumed.\n */\n onFailure?: (response: Response) => any\n\n /**\n * The callback that is called when the request is complete. This callback will be called\n * after the request is complete and all data has been received.\n */\n onEnd?: (response: Response) => any\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 */\nexport async function request(route: string, options?: RequestOptions): Promise<unknown>\nexport async function request(route: string, options: RequestOptions = {}): Promise<unknown> {\n const response = await fetch(route, options)\n return await handleResponse(response, options)\n}\n"],"names":["parseRequest","handleResponse"],"mappings":";;AAaA,eAAsB,MAAM,OAAe,UAAwB,IAAuB;AACxF,QAAM,EAAE,KAAK,KAAA,IAASA,eAAAA,aAAa,OAAO,OAAO;AACjD,MAAI,CAAC,IAAW,OAAA,IAAI,MAAM,6BAA6B;AACvD,SAAO,MAAM,WAAW,MAAM,KAAK,IAAI;AACzC;ACiDA,eAAsB,QAAQ,OAAe,UAA0B,IAAsB;AAC3F,QAAM,WAAW,MAAM,MAAM,OAAO,OAAO;AACpC,SAAA,MAAMC,eAAAA,eAAe,UAAU,OAAO;AAC/C;;;"}
|
package/dist/chunks/xJaQKKe2.js
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
import { p as parseRequest, h as handleResponse } from "./uqfvs89o.js";
|
2
|
-
async function fetch(route, options = {}) {
|
3
|
-
const { url, init } = parseRequest(route, options);
|
4
|
-
if (!url) throw new Error("Could not parse request URL");
|
5
|
-
return await globalThis.fetch(url, init);
|
6
|
-
}
|
7
|
-
async function request(route, options = {}) {
|
8
|
-
const response = await fetch(route, options);
|
9
|
-
return await handleResponse(response, options);
|
10
|
-
}
|
11
|
-
export {
|
12
|
-
fetch as f,
|
13
|
-
request as r
|
14
|
-
};
|
15
|
-
//# sourceMappingURL=xJaQKKe2.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"xJaQKKe2.js","sources":["../../utils/fetch.ts","../../utils/request.ts"],"sourcesContent":["import type { FetchOptions } from './parseRequest'\nimport { parseRequest } from './parseRequest'\n\n/**\n * Fetch a route with the provided options. This function will parse the route and\n * options to create a `Request` object and return the response from the server.\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 * @example fetch('GET /users', { query: { limit: 10 } })\n */\nexport async function fetch(route: string, options?: FetchOptions): Promise<Response>\nexport async function fetch(route: string, options: FetchOptions = {}): Promise<Response> {\n const { url, init } = parseRequest(route, options)\n if (!url) throw new Error('Could not parse request URL')\n return await globalThis.fetch(url, init)\n}\n","import type { ObjectLike } from '@unshared/types'\nimport type { FetchMethod, FetchOptions } from './parseRequest'\nimport { fetch } from './fetch'\nimport { handleResponse } from './handleResponse'\n\nexport interface RequestOptions<\n Method extends FetchMethod = FetchMethod,\n BaseUrl extends string = string,\n Parameters extends ObjectLike = ObjectLike,\n Query extends ObjectLike = ObjectLike,\n Body = unknown,\n Headers extends ObjectLike = ObjectLike,\n Data = any,\n Response = globalThis.Response,\n> extends\n FetchOptions<Method, BaseUrl, Parameters, Query, Body, Headers> {\n\n /**\n * The callback that is called when an error occurs during the request.\n */\n onError?: (error: Error) => any\n\n /**\n * The callback that is called when data is received from the request. This callback\n * will be called for each chunk of data that is received from the request.\n */\n onData?: (data: Data) => any\n\n /**\n * The callback that is called when the request is successful. This callback will be\n * called after the request is complete and all data has been received.\n */\n onSuccess?: (response: Response) => any\n\n /**\n * The callback that is called when the status code is not OK. This callback will be called\n * after the request is complete and before the data is consumed.\n */\n onFailure?: (response: Response) => any\n\n /**\n * The callback that is called when the request is complete. This callback will be called\n * after the request is complete and all data has been received.\n */\n onEnd?: (response: Response) => any\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 */\nexport async function request(route: string, options?: RequestOptions): Promise<unknown>\nexport async function request(route: string, options: RequestOptions = {}): Promise<unknown> {\n const response = await fetch(route, options)\n return await handleResponse(response, options)\n}\n"],"names":[],"mappings":";AAaA,eAAsB,MAAM,OAAe,UAAwB,IAAuB;AACxF,QAAM,EAAE,KAAK,KAAA,IAAS,aAAa,OAAO,OAAO;AACjD,MAAI,CAAC,IAAW,OAAA,IAAI,MAAM,6BAA6B;AACvD,SAAO,MAAM,WAAW,MAAM,KAAK,IAAI;AACzC;ACiDA,eAAsB,QAAQ,OAAe,UAA0B,IAAsB;AAC3F,QAAM,WAAW,MAAM,MAAM,OAAO,OAAO;AACpC,SAAA,MAAM,eAAe,UAAU,OAAO;AAC/C;"}
|