File size: 1,862 Bytes
1477a90 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | // Code generated by @openmeter/typespec-typescript. DO NOT EDIT.
import ky, { type KyInstance as HTTPClient } from 'ky'
import { type SDKOptions } from './lib/config.js'
import { encodePath } from './lib/encodings.js'
import { SDK_VERSION } from './lib/version.js'
export class Client {
readonly _options: SDKOptions
readonly _http: HTTPClient
constructor(options: SDKOptions) {
this._options = options
let baseUrl =
typeof options.baseUrl === 'string'
? encodePath(options.baseUrl, options.serverVariables ?? {})
: String(options.baseUrl)
if (!baseUrl.endsWith('/')) {
baseUrl = `${baseUrl}/`
}
this._http = ky.create({
...options,
baseUrl,
prefix: undefined,
hooks: {
...options.hooks,
beforeRequest: [
...(options.hooks?.beforeRequest ?? []),
async ({ request }) => {
// User-Agent is settable on the web but is not CORS-safelisted, so
// setting it would preflight otherwise-simple cross-origin requests.
// Gate on a positive Node check, not `window`: workers are also
// CORS-bound yet have no `window`.
if (
typeof process !== 'undefined' &&
process.versions?.node != null &&
!request.headers.has('User-Agent')
) {
request.headers.set('User-Agent', `openmeter-node/${SDK_VERSION}`)
}
const token =
typeof options.apiKey === 'function'
? await options.apiKey()
: options.apiKey
if (token) {
request.headers.set('Authorization', `Bearer ${token}`)
}
},
],
},
})
}
}
export function http(client: Client): HTTPClient {
return client._http
}
export type { HTTPClient }
|