From ca031278ca9ca30277620e344f7a95c597a8a0de Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Sat, 2 Aug 2025 18:50:19 -0400 Subject: wip: plugins --- packages/sdk/js/src/gen/core/auth.ts | 27 ++-- packages/sdk/js/src/gen/core/bodySerializer.ts | 86 +++++------- packages/sdk/js/src/gen/core/params.ts | 121 ++++++++--------- packages/sdk/js/src/gen/core/pathSerializer.ts | 174 ++++++++++++------------- packages/sdk/js/src/gen/core/types.ts | 85 ++++-------- 5 files changed, 211 insertions(+), 282 deletions(-) (limited to 'packages/sdk/js/src/gen/core') diff --git a/packages/sdk/js/src/gen/core/auth.ts b/packages/sdk/js/src/gen/core/auth.ts index 451c7f30f..e496d4557 100644 --- a/packages/sdk/js/src/gen/core/auth.ts +++ b/packages/sdk/js/src/gen/core/auth.ts @@ -1,4 +1,4 @@ -export type AuthToken = string | undefined; +export type AuthToken = string | undefined export interface Auth { /** @@ -6,35 +6,34 @@ export interface Auth { * * @default 'header' */ - in?: 'header' | 'query' | 'cookie'; + in?: "header" | "query" | "cookie" /** * Header or query parameter name. * * @default 'Authorization' */ - name?: string; - scheme?: 'basic' | 'bearer'; - type: 'apiKey' | 'http'; + name?: string + scheme?: "basic" | "bearer" + type: "apiKey" | "http" } export const getAuthToken = async ( auth: Auth, callback: ((auth: Auth) => Promise | AuthToken) | AuthToken, ): Promise => { - const token = - typeof callback === 'function' ? await callback(auth) : callback; + const token = typeof callback === "function" ? await callback(auth) : callback if (!token) { - return; + return } - if (auth.scheme === 'bearer') { - return `Bearer ${token}`; + if (auth.scheme === "bearer") { + return `Bearer ${token}` } - if (auth.scheme === 'basic') { - return `Basic ${btoa(token)}`; + if (auth.scheme === "basic") { + return `Basic ${btoa(token)}` } - return token; -}; + return token +} diff --git a/packages/sdk/js/src/gen/core/bodySerializer.ts b/packages/sdk/js/src/gen/core/bodySerializer.ts index 98ce7791f..45b2e9943 100644 --- a/packages/sdk/js/src/gen/core/bodySerializer.ts +++ b/packages/sdk/js/src/gen/core/bodySerializer.ts @@ -1,88 +1,70 @@ -import type { - ArrayStyle, - ObjectStyle, - SerializerOptions, -} from './pathSerializer'; +import type { ArrayStyle, ObjectStyle, SerializerOptions } from "./pathSerializer" -export type QuerySerializer = (query: Record) => string; +export type QuerySerializer = (query: Record) => string -export type BodySerializer = (body: any) => any; +export type BodySerializer = (body: any) => any export interface QuerySerializerOptions { - allowReserved?: boolean; - array?: SerializerOptions; - object?: SerializerOptions; + allowReserved?: boolean + array?: SerializerOptions + object?: SerializerOptions } -const serializeFormDataPair = ( - data: FormData, - key: string, - value: unknown, -): void => { - if (typeof value === 'string' || value instanceof Blob) { - data.append(key, value); +const serializeFormDataPair = (data: FormData, key: string, value: unknown): void => { + if (typeof value === "string" || value instanceof Blob) { + data.append(key, value) } else { - data.append(key, JSON.stringify(value)); + data.append(key, JSON.stringify(value)) } -}; +} -const serializeUrlSearchParamsPair = ( - data: URLSearchParams, - key: string, - value: unknown, -): void => { - if (typeof value === 'string') { - data.append(key, value); +const serializeUrlSearchParamsPair = (data: URLSearchParams, key: string, value: unknown): void => { + if (typeof value === "string") { + data.append(key, value) } else { - data.append(key, JSON.stringify(value)); + data.append(key, JSON.stringify(value)) } -}; +} export const formDataBodySerializer = { - bodySerializer: | Array>>( - body: T, - ): FormData => { - const data = new FormData(); + bodySerializer: | Array>>(body: T): FormData => { + const data = new FormData() Object.entries(body).forEach(([key, value]) => { if (value === undefined || value === null) { - return; + return } if (Array.isArray(value)) { - value.forEach((v) => serializeFormDataPair(data, key, v)); + value.forEach((v) => serializeFormDataPair(data, key, v)) } else { - serializeFormDataPair(data, key, value); + serializeFormDataPair(data, key, value) } - }); + }) - return data; + return data }, -}; +} export const jsonBodySerializer = { bodySerializer: (body: T): string => - JSON.stringify(body, (_key, value) => - typeof value === 'bigint' ? value.toString() : value, - ), -}; + JSON.stringify(body, (_key, value) => (typeof value === "bigint" ? value.toString() : value)), +} export const urlSearchParamsBodySerializer = { - bodySerializer: | Array>>( - body: T, - ): string => { - const data = new URLSearchParams(); + bodySerializer: | Array>>(body: T): string => { + const data = new URLSearchParams() Object.entries(body).forEach(([key, value]) => { if (value === undefined || value === null) { - return; + return } if (Array.isArray(value)) { - value.forEach((v) => serializeUrlSearchParamsPair(data, key, v)); + value.forEach((v) => serializeUrlSearchParamsPair(data, key, v)) } else { - serializeUrlSearchParamsPair(data, key, value); + serializeUrlSearchParamsPair(data, key, value) } - }); + }) - return data.toString(); + return data.toString() }, -}; +} diff --git a/packages/sdk/js/src/gen/core/params.ts b/packages/sdk/js/src/gen/core/params.ts index ba35263d8..0a09619d1 100644 --- a/packages/sdk/js/src/gen/core/params.ts +++ b/packages/sdk/js/src/gen/core/params.ts @@ -1,142 +1,133 @@ -type Slot = 'body' | 'headers' | 'path' | 'query'; +type Slot = "body" | "headers" | "path" | "query" export type Field = | { - in: Exclude; + in: Exclude /** * Field name. This is the name we want the user to see and use. */ - key: string; + key: string /** * Field mapped name. This is the name we want to use in the request. * If omitted, we use the same value as `key`. */ - map?: string; + map?: string } | { - in: Extract; + in: Extract /** * Key isn't required for bodies. */ - key?: string; - map?: string; - }; + key?: string + map?: string + } export interface Fields { - allowExtra?: Partial>; - args?: ReadonlyArray; + allowExtra?: Partial> + args?: ReadonlyArray } -export type FieldsConfig = ReadonlyArray; +export type FieldsConfig = ReadonlyArray const extraPrefixesMap: Record = { - $body_: 'body', - $headers_: 'headers', - $path_: 'path', - $query_: 'query', -}; -const extraPrefixes = Object.entries(extraPrefixesMap); + $body_: "body", + $headers_: "headers", + $path_: "path", + $query_: "query", +} +const extraPrefixes = Object.entries(extraPrefixesMap) type KeyMap = Map< string, { - in: Slot; - map?: string; + in: Slot + map?: string } ->; +> const buildKeyMap = (fields: FieldsConfig, map?: KeyMap): KeyMap => { if (!map) { - map = new Map(); + map = new Map() } for (const config of fields) { - if ('in' in config) { + if ("in" in config) { if (config.key) { map.set(config.key, { in: config.in, map: config.map, - }); + }) } } else if (config.args) { - buildKeyMap(config.args, map); + buildKeyMap(config.args, map) } } - return map; -}; + return map +} interface Params { - body: unknown; - headers: Record; - path: Record; - query: Record; + body: unknown + headers: Record + path: Record + query: Record } const stripEmptySlots = (params: Params) => { for (const [slot, value] of Object.entries(params)) { - if (value && typeof value === 'object' && !Object.keys(value).length) { - delete params[slot as Slot]; + if (value && typeof value === "object" && !Object.keys(value).length) { + delete params[slot as Slot] } } -}; +} -export const buildClientParams = ( - args: ReadonlyArray, - fields: FieldsConfig, -) => { +export const buildClientParams = (args: ReadonlyArray, fields: FieldsConfig) => { const params: Params = { body: {}, headers: {}, path: {}, query: {}, - }; + } - const map = buildKeyMap(fields); + const map = buildKeyMap(fields) - let config: FieldsConfig[number] | undefined; + let config: FieldsConfig[number] | undefined for (const [index, arg] of args.entries()) { if (fields[index]) { - config = fields[index]; + config = fields[index] } if (!config) { - continue; + continue } - if ('in' in config) { + if ("in" in config) { if (config.key) { - const field = map.get(config.key)!; - const name = field.map || config.key; - (params[field.in] as Record)[name] = arg; + const field = map.get(config.key)! + const name = field.map || config.key + ;(params[field.in] as Record)[name] = arg } else { - params.body = arg; + params.body = arg } } else { for (const [key, value] of Object.entries(arg ?? {})) { - const field = map.get(key); + const field = map.get(key) if (field) { - const name = field.map || key; - (params[field.in] as Record)[name] = value; + const name = field.map || key + ;(params[field.in] as Record)[name] = value } else { - const extra = extraPrefixes.find(([prefix]) => - key.startsWith(prefix), - ); + const extra = extraPrefixes.find(([prefix]) => key.startsWith(prefix)) if (extra) { - const [prefix, slot] = extra; - (params[slot] as Record)[ - key.slice(prefix.length) - ] = value; + const [prefix, slot] = extra + ;(params[slot] as Record)[key.slice(prefix.length)] = value } else { - for (const [slot, allowed] of Object.entries( - config.allowExtra ?? {}, - )) { + for (const [slot, allowed] of Object.entries(config.allowExtra ?? {})) { if (allowed) { - (params[slot as Slot] as Record)[key] = value; - break; + ;(params[slot as Slot] as Record)[key] = value + break } } } @@ -145,7 +136,7 @@ export const buildClientParams = ( } } - stripEmptySlots(params); + stripEmptySlots(params) - return params; -}; + return params +} diff --git a/packages/sdk/js/src/gen/core/pathSerializer.ts b/packages/sdk/js/src/gen/core/pathSerializer.ts index d692cf0a3..1e27c8d18 100644 --- a/packages/sdk/js/src/gen/core/pathSerializer.ts +++ b/packages/sdk/js/src/gen/core/pathSerializer.ts @@ -1,68 +1,66 @@ -interface SerializeOptions - extends SerializePrimitiveOptions, - SerializerOptions {} +interface SerializeOptions extends SerializePrimitiveOptions, SerializerOptions {} interface SerializePrimitiveOptions { - allowReserved?: boolean; - name: string; + allowReserved?: boolean + name: string } export interface SerializerOptions { /** * @default true */ - explode: boolean; - style: T; + explode: boolean + style: T } -export type ArrayStyle = 'form' | 'spaceDelimited' | 'pipeDelimited'; -export type ArraySeparatorStyle = ArrayStyle | MatrixStyle; -type MatrixStyle = 'label' | 'matrix' | 'simple'; -export type ObjectStyle = 'form' | 'deepObject'; -type ObjectSeparatorStyle = ObjectStyle | MatrixStyle; +export type ArrayStyle = "form" | "spaceDelimited" | "pipeDelimited" +export type ArraySeparatorStyle = ArrayStyle | MatrixStyle +type MatrixStyle = "label" | "matrix" | "simple" +export type ObjectStyle = "form" | "deepObject" +type ObjectSeparatorStyle = ObjectStyle | MatrixStyle interface SerializePrimitiveParam extends SerializePrimitiveOptions { - value: string; + value: string } export const separatorArrayExplode = (style: ArraySeparatorStyle) => { switch (style) { - case 'label': - return '.'; - case 'matrix': - return ';'; - case 'simple': - return ','; + case "label": + return "." + case "matrix": + return ";" + case "simple": + return "," default: - return '&'; + return "&" } -}; +} export const separatorArrayNoExplode = (style: ArraySeparatorStyle) => { switch (style) { - case 'form': - return ','; - case 'pipeDelimited': - return '|'; - case 'spaceDelimited': - return '%20'; + case "form": + return "," + case "pipeDelimited": + return "|" + case "spaceDelimited": + return "%20" default: - return ','; + return "," } -}; +} export const separatorObjectExplode = (style: ObjectSeparatorStyle) => { switch (style) { - case 'label': - return '.'; - case 'matrix': - return ';'; - case 'simple': - return ','; + case "label": + return "." + case "matrix": + return ";" + case "simple": + return "," default: - return '&'; + return "&" } -}; +} export const serializeArrayParam = ({ allowReserved, @@ -71,60 +69,54 @@ export const serializeArrayParam = ({ style, value, }: SerializeOptions & { - value: unknown[]; + value: unknown[] }) => { if (!explode) { - const joinedValues = ( - allowReserved ? value : value.map((v) => encodeURIComponent(v as string)) - ).join(separatorArrayNoExplode(style)); + const joinedValues = (allowReserved ? value : value.map((v) => encodeURIComponent(v as string))).join( + separatorArrayNoExplode(style), + ) switch (style) { - case 'label': - return `.${joinedValues}`; - case 'matrix': - return `;${name}=${joinedValues}`; - case 'simple': - return joinedValues; + case "label": + return `.${joinedValues}` + case "matrix": + return `;${name}=${joinedValues}` + case "simple": + return joinedValues default: - return `${name}=${joinedValues}`; + return `${name}=${joinedValues}` } } - const separator = separatorArrayExplode(style); + const separator = separatorArrayExplode(style) const joinedValues = value .map((v) => { - if (style === 'label' || style === 'simple') { - return allowReserved ? v : encodeURIComponent(v as string); + if (style === "label" || style === "simple") { + return allowReserved ? v : encodeURIComponent(v as string) } return serializePrimitiveParam({ allowReserved, name, value: v as string, - }); + }) }) - .join(separator); - return style === 'label' || style === 'matrix' - ? separator + joinedValues - : joinedValues; -}; + .join(separator) + return style === "label" || style === "matrix" ? separator + joinedValues : joinedValues +} -export const serializePrimitiveParam = ({ - allowReserved, - name, - value, -}: SerializePrimitiveParam) => { +export const serializePrimitiveParam = ({ allowReserved, name, value }: SerializePrimitiveParam) => { if (value === undefined || value === null) { - return ''; + return "" } - if (typeof value === 'object') { + if (typeof value === "object") { throw new Error( - 'Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these.', - ); + "Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these.", + ) } - return `${name}=${allowReserved ? value : encodeURIComponent(value)}`; -}; + return `${name}=${allowReserved ? value : encodeURIComponent(value)}` +} export const serializeObjectParam = ({ allowReserved, @@ -134,46 +126,40 @@ export const serializeObjectParam = ({ value, valueOnly, }: SerializeOptions & { - value: Record | Date; - valueOnly?: boolean; + value: Record | Date + valueOnly?: boolean }) => { if (value instanceof Date) { - return valueOnly ? value.toISOString() : `${name}=${value.toISOString()}`; + return valueOnly ? value.toISOString() : `${name}=${value.toISOString()}` } - if (style !== 'deepObject' && !explode) { - let values: string[] = []; + if (style !== "deepObject" && !explode) { + let values: string[] = [] Object.entries(value).forEach(([key, v]) => { - values = [ - ...values, - key, - allowReserved ? (v as string) : encodeURIComponent(v as string), - ]; - }); - const joinedValues = values.join(','); + values = [...values, key, allowReserved ? (v as string) : encodeURIComponent(v as string)] + }) + const joinedValues = values.join(",") switch (style) { - case 'form': - return `${name}=${joinedValues}`; - case 'label': - return `.${joinedValues}`; - case 'matrix': - return `;${name}=${joinedValues}`; + case "form": + return `${name}=${joinedValues}` + case "label": + return `.${joinedValues}` + case "matrix": + return `;${name}=${joinedValues}` default: - return joinedValues; + return joinedValues } } - const separator = separatorObjectExplode(style); + const separator = separatorObjectExplode(style) const joinedValues = Object.entries(value) .map(([key, v]) => serializePrimitiveParam({ allowReserved, - name: style === 'deepObject' ? `${name}[${key}]` : key, + name: style === "deepObject" ? `${name}[${key}]` : key, value: v as string, }), ) - .join(separator); - return style === 'label' || style === 'matrix' - ? separator + joinedValues - : joinedValues; -}; + .join(separator) + return style === "label" || style === "matrix" ? separator + joinedValues : joinedValues +} diff --git a/packages/sdk/js/src/gen/core/types.ts b/packages/sdk/js/src/gen/core/types.ts index 2dd4106fb..87cc8fec9 100644 --- a/packages/sdk/js/src/gen/core/types.ts +++ b/packages/sdk/js/src/gen/core/types.ts @@ -1,32 +1,23 @@ -import type { Auth, AuthToken } from './auth'; -import type { - BodySerializer, - QuerySerializer, - QuerySerializerOptions, -} from './bodySerializer'; +import type { Auth, AuthToken } from "./auth" +import type { BodySerializer, QuerySerializer, QuerySerializerOptions } from "./bodySerializer" -export interface Client< - RequestFn = never, - Config = unknown, - MethodFn = never, - BuildUrlFn = never, -> { +export interface Client { /** * Returns the final request URL. */ - buildUrl: BuildUrlFn; - connect: MethodFn; - delete: MethodFn; - get: MethodFn; - getConfig: () => Config; - head: MethodFn; - options: MethodFn; - patch: MethodFn; - post: MethodFn; - put: MethodFn; - request: RequestFn; - setConfig: (config: Config) => Config; - trace: MethodFn; + buildUrl: BuildUrlFn + connect: MethodFn + delete: MethodFn + get: MethodFn + getConfig: () => Config + head: MethodFn + options: MethodFn + patch: MethodFn + post: MethodFn + put: MethodFn + request: RequestFn + setConfig: (config: Config) => Config + trace: MethodFn } export interface Config { @@ -34,12 +25,12 @@ export interface Config { * Auth token or a function returning auth token. The resolved value will be * added to the request payload as defined by its `security` array. */ - auth?: ((auth: Auth) => Promise | AuthToken) | AuthToken; + auth?: ((auth: Auth) => Promise | AuthToken) | AuthToken /** * A function for serializing request body parameter. By default, * {@link JSON.stringify()} will be used. */ - bodySerializer?: BodySerializer | null; + bodySerializer?: BodySerializer | null /** * An object containing any HTTP headers that you want to pre-populate your * `Headers` object with. @@ -47,32 +38,14 @@ export interface Config { * {@link https://developer.mozilla.org/docs/Web/API/Headers/Headers#init See more} */ headers?: - | RequestInit['headers'] - | Record< - string, - | string - | number - | boolean - | (string | number | boolean)[] - | null - | undefined - | unknown - >; + | RequestInit["headers"] + | Record /** * The request method. * * {@link https://developer.mozilla.org/docs/Web/API/fetch#method See more} */ - method?: - | 'CONNECT' - | 'DELETE' - | 'GET' - | 'HEAD' - | 'OPTIONS' - | 'PATCH' - | 'POST' - | 'PUT' - | 'TRACE'; + method?: "CONNECT" | "DELETE" | "GET" | "HEAD" | "OPTIONS" | "PATCH" | "POST" | "PUT" | "TRACE" /** * A function for serializing request query parameters. By default, arrays * will be exploded in form style, objects will be exploded in deepObject @@ -83,24 +56,24 @@ export interface Config { * * {@link https://swagger.io/docs/specification/serialization/#query View examples} */ - querySerializer?: QuerySerializer | QuerySerializerOptions; + querySerializer?: QuerySerializer | QuerySerializerOptions /** * A function validating request data. This is useful if you want to ensure * the request conforms to the desired shape, so it can be safely sent to * the server. */ - requestValidator?: (data: unknown) => Promise; + requestValidator?: (data: unknown) => Promise /** * A function transforming response data before it's returned. This is useful * for post-processing data, e.g. converting ISO strings into Date objects. */ - responseTransformer?: (data: unknown) => Promise; + responseTransformer?: (data: unknown) => Promise /** * A function validating response data. This is useful if you want to ensure * the response conforms to the desired shape, so it can be safely passed to * the transformers and returned to the user. */ - responseValidator?: (data: unknown) => Promise; + responseValidator?: (data: unknown) => Promise } type IsExactlyNeverOrNeverUndefined = [T] extends [never] @@ -109,10 +82,8 @@ type IsExactlyNeverOrNeverUndefined = [T] extends [never] ? [undefined] extends [T] ? false : true - : false; + : false export type OmitNever> = { - [K in keyof T as IsExactlyNeverOrNeverUndefined extends true - ? never - : K]: T[K]; -}; + [K in keyof T as IsExactlyNeverOrNeverUndefined extends true ? never : K]: T[K] +} -- cgit v1.2.3