cloudflare
Version:
The official TypeScript library for the Cloudflare API
59 lines • 2.04 kB
JavaScript
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { BaseCloudflare } from "./client.mjs";
import { APIResource } from "./core/resource.mjs";
import { hasOwn } from "./internal/utils/values.mjs";
/**
* Creates a client with a subset of the available resources to reduce
* bundle size.
* Import the resource classes you need from `cloudflare/resources/*`.
* Use the BaseResource variants if you do not need to use subresources.
*
* @example
* ```ts
* import { Accounts } from `cloudflare/resources/accounts`;
* import { createClient } from `cloudflare/tree-shakable`;
*
* const client = createClient({
* resources: [Accounts],
* });
* ```
*/
export function createClient(options) {
const client = new BaseCloudflare(options);
for (const ResourceClass of options.resources) {
const resourceInstance = new ResourceClass(client);
let object = client;
for (const part of ResourceClass._key.slice(0, -1)) {
if (hasOwn(object, part)) {
object = object[part];
}
else {
Object.defineProperty(object, part, {
value: (object = {}),
configurable: true,
enumerable: true,
writable: true,
});
}
}
const name = ResourceClass._key.at(-1);
if (!hasOwn(object, name)) {
Object.defineProperty(object, name, {
value: resourceInstance,
configurable: true,
enumerable: true,
writable: true,
});
}
else {
if (object[name] instanceof APIResource) {
throw new TypeError(`Resource at ${ResourceClass._key.join('.')} already exists!`);
}
else {
object[name] = Object.assign(resourceInstance, object[name]);
}
}
}
return client;
}
//# sourceMappingURL=tree-shakable.mjs.map