Skip to content

Commit

Permalink
remove linting since most code is generated
Browse files Browse the repository at this point in the history
  • Loading branch information
DarrenBaldwin07 committed Dec 3, 2024
1 parent 92f16a2 commit 38eabdb
Show file tree
Hide file tree
Showing 8 changed files with 1,011 additions and 66 deletions.
7 changes: 0 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ jobs:
- uses: ./.github/actions/prepare
- run: pnpm build
- run: node ./lib/index.js
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/prepare
- run: pnpm lint
prettier:
name: Prettier
runs-on: ubuntu-latest
Expand Down
31 changes: 0 additions & 31 deletions eslint.config.js

This file was deleted.

2 changes: 1 addition & 1 deletion openapi-ts.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from "@hey-api/openapi-ts";

export default defineConfig({
client: "@hey-api/client-fetch",
client: { bundle: true, name: "@hey-api/client-fetch" },
input: "https://api.tembo.io/api-docs/openapi.json",
output: "src/",
});
181 changes: 181 additions & 0 deletions src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import type { Client, Config, RequestOptions } from "./types.js";
import {
createConfig,
createInterceptors,
createQuerySerializer,
getParseAs,
getUrl,
mergeConfigs,
mergeHeaders,
} from "./utils.js";

type ReqInit = Omit<RequestInit, "body" | "headers"> & {
body?: any;
headers: ReturnType<typeof mergeHeaders>;
};

export const createClient = (config: Config = {}): Client => {
let _config = mergeConfigs(createConfig(), config);

const getConfig = (): Config => ({ ..._config });

const setConfig = (config: Config): Config => {
_config = mergeConfigs(_config, config);
return getConfig();
};

const buildUrl: Client["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;
};

const interceptors = createInterceptors<
Request,
Response,
unknown,
RequestOptions
>();

// @ts-expect-error
const request: Client["request"] = async (options) => {
// @ts-ignore
const opts: RequestOptions = {
..._config,
...options,
headers: mergeHeaders(_config.headers, options.headers),
};
if (opts.body && opts.bodySerializer) {
opts.body = opts.bodySerializer(opts.body);
}

// remove Content-Type header if body is empty to avoid sending invalid requests
if (!opts.body) {
opts.headers.delete("Content-Type");
}

const url = buildUrl(opts);
const requestInit: ReqInit = {
redirect: "follow",
...opts,
};

let request = new Request(url, requestInit);

for (const fn of interceptors.request._fns) {
request = await fn(request, opts);
}

const _fetch = opts.fetch!;
let response = await _fetch(request);

for (const fn of interceptors.response._fns) {
response = await fn(response, request, opts);
}

const result = {
request,
response,
};

if (response.ok) {
if (
response.status === 204 ||
response.headers.get("Content-Length") === "0"
) {
return {
data: {},
...result,
};
}

if (opts.parseAs === "stream") {
return {
data: response.body,
...result,
};
}

const parseAs =
(opts.parseAs === "auto"
? getParseAs(response.headers.get("Content-Type"))
: opts.parseAs) ?? "json";
// @ts-ignore
let data = await response[parseAs]();
if (parseAs === "json" && opts.responseTransformer) {
data = await opts.responseTransformer(data);
}

return {
data,
...result,
};
}

let error = await response.text();

try {
error = JSON.parse(error);
} catch {
// noop
}

let finalError = error;

for (const fn of interceptors.error._fns) {
finalError = (await fn(error, response, request, opts)) as string;
}

finalError = finalError || ({} as string);

if (opts.throwOnError) {
throw finalError;
}

return {
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 type {
Client,
Config,
Options,
OptionsLegacyParser,
RequestOptionsBase,
RequestResult,
} from "./types.ts";
export {
createConfig,
formDataBodySerializer,
jsonBodySerializer,
type QuerySerializerOptions,
urlSearchParamsBodySerializer,
} from "./utils.ts";
Loading

0 comments on commit 38eabdb

Please sign in to comment.