-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove linting since most code is generated
- Loading branch information
1 parent
92f16a2
commit 38eabdb
Showing
8 changed files
with
1,011 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
Oops, something went wrong.