@nekofar/warpcast
Version:
TypeScript client for interacting with Warpcast APIs
607 lines (599 loc) • 15.8 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/client/client.gen.ts
var client_gen_exports = {};
__export(client_gen_exports, {
client: () => client
});
module.exports = __toCommonJS(client_gen_exports);
// src/client/core/bodySerializer.ts
var jsonBodySerializer = {
bodySerializer: (body) => JSON.stringify(
body,
(_key, value) => typeof value === "bigint" ? value.toString() : value
)
};
// src/client/core/auth.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/client/core/pathSerializer.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/client/client/utils.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() {
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/client/client/client.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)
};
if (opts.security) {
await setAuthParams({
...opts,
security: opts.security
});
}
if (opts.requestValidator) {
await opts.requestValidator(opts);
}
if (opts.body && opts.bodySerializer) {
opts.body = opts.bodySerializer(opts.body);
}
if (opts.body === void 0 || opts.body === "") {
opts.headers.delete("Content-Type");
}
const url = buildUrl(opts);
const requestInit = {
redirect: "follow",
...opts
};
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" })
};
};
// src/client/client.gen.ts
var client = createClient(
createConfig({
baseUrl: "https://api.farcaster.xyz"
})
);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
client
});