UNPKG

@nextplus/js-sdk

Version:

A TypeScript SDK for interacting with the NextPlus API, automatically generated from OpenAPI specifications.

727 lines (719 loc) 18.8 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); // src/sdk/core/bodySerializer.gen.ts var serializeFormDataPair = (data, key, value) => { if (typeof value === "string" || value instanceof Blob) { data.append(key, value); } else { data.append(key, JSON.stringify(value)); } }; var serializeUrlSearchParamsPair = (data, key, value) => { if (typeof value === "string") { data.append(key, value); } else { data.append(key, JSON.stringify(value)); } }; var formDataBodySerializer = { bodySerializer: (body) => { const data = new FormData(); Object.entries(body).forEach(([key, value]) => { if (value === void 0 || value === null) { return; } if (Array.isArray(value)) { value.forEach((v) => serializeFormDataPair(data, key, v)); } else { serializeFormDataPair(data, key, value); } }); return data; } }; var jsonBodySerializer = { bodySerializer: (body) => JSON.stringify( body, (_key, value) => typeof value === "bigint" ? value.toString() : value ) }; var urlSearchParamsBodySerializer = { bodySerializer: (body) => { const data = new URLSearchParams(); Object.entries(body).forEach(([key, value]) => { if (value === void 0 || value === null) { return; } if (Array.isArray(value)) { value.forEach((v) => serializeUrlSearchParamsPair(data, key, v)); } else { serializeUrlSearchParamsPair(data, key, value); } }); return data.toString(); } }; // src/sdk/core/params.gen.ts var extraPrefixesMap = { $body_: "body", $headers_: "headers", $path_: "path", $query_: "query" }; var extraPrefixes = Object.entries(extraPrefixesMap); var buildKeyMap = (fields, map) => { if (!map) { map = /* @__PURE__ */ new Map(); } for (const config of fields) { 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); } } return map; }; var stripEmptySlots = (params) => { for (const [slot, value] of Object.entries(params)) { if (value && typeof value === "object" && !Object.keys(value).length) { delete params[slot]; } } }; var buildClientParams = (args, fields) => { const params = { body: {}, headers: {}, path: {}, query: {} }; const map = buildKeyMap(fields); let config; for (const [index, arg] of args.entries()) { if (fields[index]) { config = fields[index]; } if (!config) { continue; } if ("in" in config) { if (config.key) { const field = map.get(config.key); const name = field.map || config.key; params[field.in][name] = arg; } else { params.body = arg; } } else { for (const [key, value] of Object.entries(arg ?? {})) { const field = map.get(key); if (field) { const name = field.map || key; params[field.in][name] = value; } else { const extra = extraPrefixes.find( ([prefix]) => key.startsWith(prefix) ); if (extra) { const [prefix, slot] = extra; params[slot][key.slice(prefix.length)] = value; } else { for (const [slot, allowed] of Object.entries( config.allowExtra ?? {} )) { if (allowed) { params[slot][key] = value; break; } } } } } } } stripEmptySlots(params); return params; }; // src/sdk/core/auth.gen.ts var getAuthToken = async (auth, callback) => { const token = typeof callback === "function" ? await callback(auth) : callback; if (!token) { return; } if (auth.scheme === "bearer") { return `Bearer ${token}`; } if (auth.scheme === "basic") { return `Basic ${btoa(token)}`; } return token; }; // src/sdk/core/pathSerializer.gen.ts var separatorArrayExplode = (style) => { switch (style) { case "label": return "."; case "matrix": return ";"; case "simple": return ","; default: return "&"; } }; var separatorArrayNoExplode = (style) => { switch (style) { case "form": return ","; case "pipeDelimited": return "|"; case "spaceDelimited": return "%20"; default: return ","; } }; var separatorObjectExplode = (style) => { switch (style) { case "label": return "."; case "matrix": return ";"; case "simple": return ","; default: return "&"; } }; var serializeArrayParam = ({ allowReserved, explode, name, style, value }) => { if (!explode) { const joinedValues2 = (allowReserved ? value : value.map((v) => encodeURIComponent(v))).join(separatorArrayNoExplode(style)); switch (style) { case "label": return `.${joinedValues2}`; case "matrix": return `;${name}=${joinedValues2}`; case "simple": return joinedValues2; default: return `${name}=${joinedValues2}`; } } const separator = separatorArrayExplode(style); const joinedValues = value.map((v) => { if (style === "label" || style === "simple") { return allowReserved ? v : encodeURIComponent(v); } return serializePrimitiveParam({ allowReserved, name, value: v }); }).join(separator); return style === "label" || style === "matrix" ? separator + joinedValues : joinedValues; }; var serializePrimitiveParam = ({ allowReserved, name, value }) => { if (value === void 0 || value === null) { return ""; } if (typeof value === "object") { throw new Error( "Deeply-nested arrays/objects aren\u2019t supported. Provide your own `querySerializer()` to handle these." ); } return `${name}=${allowReserved ? value : encodeURIComponent(value)}`; }; var serializeObjectParam = ({ allowReserved, explode, name, style, value, valueOnly }) => { if (value instanceof Date) { return valueOnly ? value.toISOString() : `${name}=${value.toISOString()}`; } if (style !== "deepObject" && !explode) { let values = []; Object.entries(value).forEach(([key, v]) => { values = [ ...values, key, allowReserved ? v : encodeURIComponent(v) ]; }); const joinedValues2 = values.join(","); switch (style) { case "form": return `${name}=${joinedValues2}`; case "label": return `.${joinedValues2}`; case "matrix": return `;${name}=${joinedValues2}`; default: return joinedValues2; } } const separator = separatorObjectExplode(style); const joinedValues = Object.entries(value).map( ([key, v]) => serializePrimitiveParam({ allowReserved, name: style === "deepObject" ? `${name}[${key}]` : key, value: v }) ).join(separator); return style === "label" || style === "matrix" ? separator + joinedValues : joinedValues; }; // src/sdk/client/utils.gen.ts var PATH_PARAM_RE = /\{[^{}]+\}/g; var defaultPathSerializer = ({ path, url: _url }) => { 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 = "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 === void 0 || 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, valueOnly: true }) ); continue; } if (style === "matrix") { url = url.replace( match, `;${serializePrimitiveParam({ name, value })}` ); continue; } const replaceValue = encodeURIComponent( style === "label" ? `.${value}` : value ); url = url.replace(match, replaceValue); } } return url; }; var createQuerySerializer = ({ allowReserved, array, object } = {}) => { const querySerializer = (queryParams) => { const search = []; if (queryParams && typeof queryParams === "object") { for (const name in queryParams) { const value = queryParams[name]; if (value === void 0 || value === null) { continue; } if (Array.isArray(value)) { const serializedArray = serializeArrayParam({ allowReserved, explode: true, name, style: "form", value, ...array }); if (serializedArray) search.push(serializedArray); } else if (typeof value === "object") { const serializedObject = serializeObjectParam({ allowReserved, explode: true, name, style: "deepObject", value, ...object }); if (serializedObject) search.push(serializedObject); } else { const serializedPrimitive = serializePrimitiveParam({ allowReserved, name, value }); if (serializedPrimitive) search.push(serializedPrimitive); } } } return search.join("&"); }; return querySerializer; }; var getParseAs = (contentType) => { if (!contentType) { return "stream"; } const cleanContent = contentType.split(";")[0]?.trim(); if (!cleanContent) { return; } if (cleanContent.startsWith("application/json") || cleanContent.endsWith("+json")) { return "json"; } if (cleanContent === "multipart/form-data") { return "formData"; } if (["application/", "audio/", "image/", "video/"].some( (type) => cleanContent.startsWith(type) )) { return "blob"; } if (cleanContent.startsWith("text/")) { return "text"; } return; }; var setAuthParams = async ({ security, ...options }) => { for (const auth of security) { const token = await getAuthToken(auth, options.auth); if (!token) { continue; } const name = auth.name ?? "Authorization"; switch (auth.in) { case "query": if (!options.query) { options.query = {}; } options.query[name] = token; break; case "cookie": options.headers.append("Cookie", `${name}=${token}`); break; case "header": default: options.headers.set(name, token); break; } return; } }; var buildUrl = (options) => { const url = getUrl({ baseUrl: options.baseUrl, path: options.path, query: options.query, querySerializer: typeof options.querySerializer === "function" ? options.querySerializer : createQuerySerializer(options.querySerializer), url: options.url }); return url; }; var getUrl = ({ baseUrl, path, query, querySerializer, url: _url }) => { 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; }; var mergeConfigs = (a, b) => { const config = { ...a, ...b }; if (config.baseUrl?.endsWith("/")) { config.baseUrl = config.baseUrl.substring(0, config.baseUrl.length - 1); } config.headers = mergeHeaders(a.headers, b.headers); return config; }; var mergeHeaders = (...headers) => { const mergedHeaders = new Headers(); for (const header of headers) { if (!header || typeof header !== "object") { continue; } const iterator = header instanceof Headers ? header.entries() : Object.entries(header); for (const [key, value] of iterator) { if (value === null) { mergedHeaders.delete(key); } else if (Array.isArray(value)) { for (const v of value) { mergedHeaders.append(key, v); } } else if (value !== void 0) { mergedHeaders.set( key, typeof value === "object" ? JSON.stringify(value) : value ); } } } return mergedHeaders; }; var Interceptors = class { constructor() { __publicField(this, "_fns"); this._fns = []; } clear() { this._fns = []; } getInterceptorIndex(id) { if (typeof id === "number") { return this._fns[id] ? id : -1; } else { return this._fns.indexOf(id); } } exists(id) { const index = this.getInterceptorIndex(id); return !!this._fns[index]; } eject(id) { const index = this.getInterceptorIndex(id); if (this._fns[index]) { this._fns[index] = null; } } update(id, fn) { const index = this.getInterceptorIndex(id); if (this._fns[index]) { this._fns[index] = fn; return id; } else { return false; } } use(fn) { this._fns = [...this._fns, fn]; return this._fns.length - 1; } }; var createInterceptors = () => ({ error: new Interceptors(), request: new Interceptors(), response: new Interceptors() }); var defaultQuerySerializer = createQuerySerializer({ allowReserved: false, array: { explode: true, style: "form" }, object: { explode: true, style: "deepObject" } }); var defaultHeaders = { "Content-Type": "application/json" }; var createConfig = (override = {}) => ({ ...jsonBodySerializer, headers: defaultHeaders, parseAs: "auto", querySerializer: defaultQuerySerializer, ...override }); // src/sdk/client/client.gen.ts var createClient = (config = {}) => { let _config = mergeConfigs(createConfig(), config); const getConfig = () => ({ ..._config }); const setConfig = (config2) => { _config = mergeConfigs(_config, config2); return getConfig(); }; const interceptors = createInterceptors(); const request = async (options) => { const opts = { ..._config, ...options, fetch: options.fetch ?? _config.fetch ?? globalThis.fetch, headers: mergeHeaders(_config.headers, options.headers), serializedBody: void 0 }; if (opts.security) { await setAuthParams({ ...opts, security: opts.security }); } if (opts.requestValidator) { await opts.requestValidator(opts); } if (opts.body && opts.bodySerializer) { opts.serializedBody = opts.bodySerializer(opts.body); } if (opts.serializedBody === void 0 || opts.serializedBody === "") { opts.headers.delete("Content-Type"); } const url = buildUrl(opts); const requestInit = { redirect: "follow", ...opts, body: opts.serializedBody }; let request2 = new Request(url, requestInit); for (const fn of interceptors.request._fns) { if (fn) { request2 = await fn(request2, opts); } } const _fetch = opts.fetch; let response = await _fetch(request2); for (const fn of interceptors.response._fns) { if (fn) { response = await fn(response, request2, opts); } } const result = { request: request2, response }; if (response.ok) { if (response.status === 204 || response.headers.get("Content-Length") === "0") { return opts.responseStyle === "data" ? {} : { data: {}, ...result }; } const parseAs = (opts.parseAs === "auto" ? getParseAs(response.headers.get("Content-Type")) : opts.parseAs) ?? "json"; let data; switch (parseAs) { case "arrayBuffer": case "blob": case "formData": case "json": case "text": data = await response[parseAs](); break; case "stream": return opts.responseStyle === "data" ? response.body : { data: response.body, ...result }; } if (parseAs === "json") { if (opts.responseValidator) { await opts.responseValidator(data); } if (opts.responseTransformer) { data = await opts.responseTransformer(data); } } return opts.responseStyle === "data" ? data : { data, ...result }; } const textError = await response.text(); let jsonError; try { jsonError = JSON.parse(textError); } catch { } const error = jsonError ?? textError; let finalError = error; for (const fn of interceptors.error._fns) { if (fn) { finalError = await fn(error, response, request2, opts); } } finalError = finalError || {}; if (opts.throwOnError) { throw finalError; } return opts.responseStyle === "data" ? void 0 : { error: finalError, ...result }; }; return { buildUrl, connect: (options) => request({ ...options, method: "CONNECT" }), delete: (options) => request({ ...options, method: "DELETE" }), get: (options) => request({ ...options, method: "GET" }), getConfig, head: (options) => request({ ...options, method: "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" }), request, setConfig, trace: (options) => request({ ...options, method: "TRACE" }) }; }; export { __export, __publicField, formDataBodySerializer, jsonBodySerializer, urlSearchParamsBodySerializer, buildClientParams, mergeHeaders, createConfig, createClient };