sdk update

This commit is contained in:
Dax Raad
2025-08-27 12:18:09 -04:00
parent 5c17ee52c5
commit 0fbd7c84fd
16 changed files with 483 additions and 175 deletions

View File

@@ -1,4 +1,7 @@
import type { Client, Config, RequestOptions } from "./types.js"
// This file is auto-generated by @hey-api/openapi-ts
import { createSseClient } from "../core/serverSentEvents.gen.js"
import type { Client, Config, RequestOptions, ResolvedRequestOptions } from "./types.gen.js"
import {
buildUrl,
createConfig,
@@ -7,7 +10,7 @@ import {
mergeConfigs,
mergeHeaders,
setAuthParams,
} from "./utils.js"
} from "./utils.gen.js"
type ReqInit = Omit<RequestInit, "body" | "headers"> & {
body?: any
@@ -24,14 +27,15 @@ export const createClient = (config: Config = {}): Client => {
return getConfig()
}
const interceptors = createInterceptors<Request, Response, unknown, RequestOptions>()
const interceptors = createInterceptors<Request, Response, unknown, ResolvedRequestOptions>()
const request: Client["request"] = async (options) => {
const beforeRequest = async (options: RequestOptions) => {
const opts = {
..._config,
...options,
fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
headers: mergeHeaders(_config.headers, options.headers),
serializedBody: undefined,
}
if (opts.security) {
@@ -46,18 +50,26 @@ export const createClient = (config: Config = {}): Client => {
}
if (opts.body && opts.bodySerializer) {
opts.body = opts.bodySerializer(opts.body)
opts.serializedBody = opts.bodySerializer(opts.body)
}
// remove Content-Type header if body is empty to avoid sending invalid requests
if (opts.body === undefined || opts.body === "") {
if (opts.serializedBody === undefined || opts.serializedBody === "") {
opts.headers.delete("Content-Type")
}
const url = buildUrl(opts)
return { opts, url }
}
const request: Client["request"] = async (options) => {
// @ts-expect-error
const { opts, url } = await beforeRequest(options)
const requestInit: ReqInit = {
redirect: "follow",
...opts,
body: opts.serializedBody,
}
let request = new Request(url, requestInit)
@@ -166,20 +178,35 @@ export const createClient = (config: Config = {}): Client => {
}
}
const makeMethod = (method: Required<Config>["method"]) => {
const fn = (options: RequestOptions) => request({ ...options, method })
fn.sse = async (options: RequestOptions) => {
const { opts, url } = await beforeRequest(options)
return createSseClient({
...opts,
body: opts.body as BodyInit | null | undefined,
headers: opts.headers as unknown as Record<string, string>,
method,
url,
})
}
return fn
}
return {
buildUrl,
connect: (options) => request({ ...options, method: "CONNECT" }),
delete: (options) => request({ ...options, method: "DELETE" }),
get: (options) => request({ ...options, method: "GET" }),
connect: makeMethod("CONNECT"),
delete: makeMethod("DELETE"),
get: makeMethod("GET"),
getConfig,
head: (options) => request({ ...options, method: "HEAD" }),
head: makeMethod("HEAD"),
interceptors,
options: (options) => request({ ...options, method: "OPTIONS" }),
patch: (options) => request({ ...options, method: "PATCH" }),
post: (options) => request({ ...options, method: "POST" }),
put: (options) => request({ ...options, method: "PUT" }),
options: makeMethod("OPTIONS"),
patch: makeMethod("PATCH"),
post: makeMethod("POST"),
put: makeMethod("PUT"),
request,
setConfig,
trace: (options) => request({ ...options, method: "TRACE" }),
}
trace: makeMethod("TRACE"),
} as Client
}

View File

@@ -1,8 +1,14 @@
export type { Auth } from "../core/auth.js"
export type { QuerySerializerOptions } from "../core/bodySerializer.js"
export { formDataBodySerializer, jsonBodySerializer, urlSearchParamsBodySerializer } from "../core/bodySerializer.js"
export { buildClientParams } from "../core/params.js"
export { createClient } from "./client.js"
// This file is auto-generated by @hey-api/openapi-ts
export type { Auth } from "../core/auth.gen.js"
export type { QuerySerializerOptions } from "../core/bodySerializer.gen.js"
export {
formDataBodySerializer,
jsonBodySerializer,
urlSearchParamsBodySerializer,
} from "../core/bodySerializer.gen.js"
export { buildClientParams } from "../core/params.gen.js"
export { createClient } from "./client.gen.js"
export type {
Client,
ClientOptions,
@@ -12,7 +18,8 @@ export type {
OptionsLegacyParser,
RequestOptions,
RequestResult,
ResolvedRequestOptions,
ResponseStyle,
TDataShape,
} from "./types.js"
export { createConfig, mergeHeaders } from "./utils.js"
} from "./types.gen.js"
export { createConfig, mergeHeaders } from "./utils.gen.js"

View File

@@ -1,6 +1,9 @@
import type { Auth } from "../core/auth.js"
import type { Client as CoreClient, Config as CoreConfig } from "../core/types.js"
import type { Middleware } from "./utils.js"
// This file is auto-generated by @hey-api/openapi-ts
import type { Auth } from "../core/auth.gen.js"
import type { ServerSentEventsOptions, ServerSentEventsResult } from "../core/serverSentEvents.gen.js"
import type { Client as CoreClient, Config as CoreConfig } from "../core/types.gen.js"
import type { Middleware } from "./utils.gen.js"
export type ResponseStyle = "data" | "fields"
@@ -49,13 +52,18 @@ export interface Config<T extends ClientOptions = ClientOptions>
}
export interface RequestOptions<
TData = unknown,
TResponseStyle extends ResponseStyle = "fields",
ThrowOnError extends boolean = boolean,
Url extends string = string,
> extends Config<{
responseStyle: TResponseStyle
throwOnError: ThrowOnError
}> {
responseStyle: TResponseStyle
throwOnError: ThrowOnError
}>,
Pick<
ServerSentEventsOptions<TData>,
"onSseError" | "onSseEvent" | "sseDefaultRetryDelay" | "sseMaxRetryAttempts" | "sseMaxRetryDelay"
> {
/**
* Any body that you want to add to your request.
*
@@ -71,6 +79,14 @@ export interface RequestOptions<
url: Url
}
export interface ResolvedRequestOptions<
TResponseStyle extends ResponseStyle = "fields",
ThrowOnError extends boolean = boolean,
Url extends string = string,
> extends RequestOptions<unknown, TResponseStyle, ThrowOnError, Url> {
serializedBody?: string
}
export type RequestResult<
TData = unknown,
TError = unknown,
@@ -112,23 +128,36 @@ export interface ClientOptions {
throwOnError?: boolean
}
type MethodFn = <
type MethodFnBase = <
TData = unknown,
TError = unknown,
ThrowOnError extends boolean = false,
TResponseStyle extends ResponseStyle = "fields",
>(
options: Omit<RequestOptions<TResponseStyle, ThrowOnError>, "method">,
options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, "method">,
) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>
type MethodFnServerSentEvents = <
TData = unknown,
TError = unknown,
ThrowOnError extends boolean = false,
TResponseStyle extends ResponseStyle = "fields",
>(
options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, "method">,
) => Promise<ServerSentEventsResult<TData, TError>>
type MethodFn = MethodFnBase & {
sse: MethodFnServerSentEvents
}
type RequestFn = <
TData = unknown,
TError = unknown,
ThrowOnError extends boolean = false,
TResponseStyle extends ResponseStyle = "fields",
>(
options: Omit<RequestOptions<TResponseStyle, ThrowOnError>, "method"> &
Pick<Required<RequestOptions<TResponseStyle, ThrowOnError>>, "method">,
options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, "method"> &
Pick<Required<RequestOptions<TData, TResponseStyle, ThrowOnError>>, "method">,
) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>
type BuildUrlFn = <
@@ -143,7 +172,7 @@ type BuildUrlFn = <
) => string
export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn> & {
interceptors: Middleware<Request, Response, unknown, RequestOptions>
interceptors: Middleware<Request, Response, unknown, ResolvedRequestOptions>
}
/**
@@ -171,8 +200,10 @@ type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>
export type Options<
TData extends TDataShape = TDataShape,
ThrowOnError extends boolean = boolean,
TResponse = unknown,
TResponseStyle extends ResponseStyle = "fields",
> = OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, "body" | "path" | "query" | "url"> & Omit<TData, "url">
> = OmitKeys<RequestOptions<TResponse, TResponseStyle, ThrowOnError>, "body" | "path" | "query" | "url"> &
Omit<TData, "url">
export type OptionsLegacyParser<
TData = unknown,
@@ -180,12 +211,12 @@ export type OptionsLegacyParser<
TResponseStyle extends ResponseStyle = "fields",
> = TData extends { body?: any }
? TData extends { headers?: any }
? OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, "body" | "headers" | "url"> & TData
: OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, "body" | "url"> &
? OmitKeys<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "body" | "headers" | "url"> & TData
: OmitKeys<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "body" | "url"> &
TData &
Pick<RequestOptions<TResponseStyle, ThrowOnError>, "headers">
Pick<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "headers">
: TData extends { headers?: any }
? OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, "headers" | "url"> &
? OmitKeys<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "headers" | "url"> &
TData &
Pick<RequestOptions<TResponseStyle, ThrowOnError>, "body">
: OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, "url"> & TData
Pick<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "body">
: OmitKeys<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "url"> & TData

View File

@@ -1,84 +1,11 @@
import { getAuthToken } from "../core/auth.js"
import type { QuerySerializer, QuerySerializerOptions } from "../core/bodySerializer.js"
import { jsonBodySerializer } from "../core/bodySerializer.js"
import { serializeArrayParam, serializeObjectParam, serializePrimitiveParam } from "../core/pathSerializer.js"
import type { Client, ClientOptions, Config, RequestOptions } from "./types.js"
// This file is auto-generated by @hey-api/openapi-ts
interface PathSerializer {
path: Record<string, unknown>
url: string
}
const PATH_PARAM_RE = /\{[^{}]+\}/g
type ArrayStyle = "form" | "spaceDelimited" | "pipeDelimited"
type MatrixStyle = "label" | "matrix" | "simple"
type ArraySeparatorStyle = ArrayStyle | MatrixStyle
const defaultPathSerializer = ({ path, url: _url }: PathSerializer) => {
let url = _url
const matches = _url.match(PATH_PARAM_RE)
if (matches) {
for (const match of matches) {
let explode = false
let name = match.substring(1, match.length - 1)
let style: ArraySeparatorStyle = "simple"
if (name.endsWith("*")) {
explode = true
name = name.substring(0, name.length - 1)
}
if (name.startsWith(".")) {
name = name.substring(1)
style = "label"
} else if (name.startsWith(";")) {
name = name.substring(1)
style = "matrix"
}
const value = path[name]
if (value === undefined || value === null) {
continue
}
if (Array.isArray(value)) {
url = url.replace(match, serializeArrayParam({ explode, name, style, value }))
continue
}
if (typeof value === "object") {
url = url.replace(
match,
serializeObjectParam({
explode,
name,
style,
value: value as Record<string, unknown>,
valueOnly: true,
}),
)
continue
}
if (style === "matrix") {
url = url.replace(
match,
`;${serializePrimitiveParam({
name,
value: value as string,
})}`,
)
continue
}
const replaceValue = encodeURIComponent(style === "label" ? `.${value as string}` : (value as string))
url = url.replace(match, replaceValue)
}
}
return url
}
import { getAuthToken } from "../core/auth.gen.js"
import type { QuerySerializerOptions } from "../core/bodySerializer.gen.js"
import { jsonBodySerializer } from "../core/bodySerializer.gen.js"
import { serializeArrayParam, serializeObjectParam, serializePrimitiveParam } from "../core/pathSerializer.gen.js"
import { getUrl } from "../core/utils.gen.js"
import type { Client, ClientOptions, Config, RequestOptions } from "./types.gen.js"
export const createQuerySerializer = <T = unknown>({ allowReserved, array, object }: QuerySerializerOptions = {}) => {
const querySerializer = (queryParams: T) => {
@@ -161,6 +88,21 @@ export const getParseAs = (contentType: string | null): Exclude<Config["parseAs"
return
}
const checkForExistence = (
options: Pick<RequestOptions, "auth" | "query"> & {
headers: Headers
},
name?: string,
): boolean => {
if (!name) {
return false
}
if (options.headers.has(name) || options.query?.[name] || options.headers.get("Cookie")?.includes(`${name}=`)) {
return true
}
return false
}
export const setAuthParams = async ({
security,
...options
@@ -169,6 +111,10 @@ export const setAuthParams = async ({
headers: Headers
}) => {
for (const auth of security) {
if (checkForExistence(options, auth.name)) {
continue
}
const token = await getAuthToken(auth, options.auth)
if (!token) {
@@ -192,13 +138,11 @@ export const setAuthParams = async ({
options.headers.set(name, token)
break
}
return
}
}
export const buildUrl: Client["buildUrl"] = (options) => {
const url = getUrl({
export const buildUrl: Client["buildUrl"] = (options) =>
getUrl({
baseUrl: options.baseUrl as string,
path: options.path,
query: options.query,
@@ -208,36 +152,6 @@ export const buildUrl: Client["buildUrl"] = (options) => {
: createQuerySerializer(options.querySerializer),
url: options.url,
})
return url
}
export const getUrl = ({
baseUrl,
path,
query,
querySerializer,
url: _url,
}: {
baseUrl?: string
path?: Record<string, unknown>
query?: Record<string, unknown>
querySerializer: QuerySerializer
url: string
}) => {
const pathUrl = _url.startsWith("/") ? _url : `/${_url}`
let url = (baseUrl ?? "") + pathUrl
if (path) {
url = defaultPathSerializer({ path, url })
}
let search = query ? querySerializer(query) : ""
if (search.startsWith("?")) {
search = search.substring(1)
}
if (search) {
url += `?${search}`
}
return url
}
export const mergeConfigs = (a: Config, b: Config): Config => {
const config = { ...a, ...b }