What is the qs package
mainqs package is a vendored version of neoqs, which is a TypeScript rewrite of the qs query string library. It is used for parsing and stringifying query strings.repository·main·Indexed 20 days ago
https://github.com/cloudflare/cloudflare-typescriptThe official TypeScript library (version 7.0.0) providing type-safe access to the Cloudflare REST API for server-side environments. It supports Node.js 20+, Deno, Bun, Cloudflare Workers, and Vercel Edge Runtime. Key features include auto-pagination, tree-shaking for reduced bundle size, configurable retries and timeouts, and comprehensive TypeScript definitions for request and response fields.
qs package is a vendored version of neoqs, which is a TypeScript rewrite of the qs query string library. It is used for parsing and stringifying query strings.v2 API (under the cloudforce-one path).Cloudflare API list methods are paginated. You have two ways to handle this:
for await ... of syntax to automatically fetch all pages as you iterate..hasNextPage() and .getNextPage() to navigate manually.// Option 1: Auto-pagination
async function fetchAllAccounts(params) {
const allAccounts = [];
for await (const account of client.accounts.list()) {
allAccounts.push(account);
}
return allAccounts;
}
// Option 2: Manual pagination
let page = await client.accounts.list();
for (const account of page.result) {
console.log(account);
}
while (page.hasNextPage()) {
page = await page.getNextPage();
// ...
}To reduce bundle size, you can create a tree-shakable client that only includes the specific API resources you need. This is done by importing createClient from cloudflare/tree-shakable and providing a resources array.
Each API resource has two versions:
Zones): Includes all subresources.BaseZones): Does not include subresources.The tree-shaken client is fully typed. You can use the PartialCloudflare type to explicitly type variables or function parameters for a client containing specific resources.
import { createClient } from 'cloudflare/tree-shakable';
import { Zones } from 'cloudflare/resources/zones/zones';
import { BaseAccounts } from 'cloudflare/resources/accounts/accounts';
const client = createClient({
resources: [Zones, BaseAccounts],
});
// The client is fully typed
const zone = await client.zones.create({
account: { id: '...' },
name: 'example.com',
});The library now uses the built-in Web fetch API across all platforms. If your code relies on node-fetch-specific properties, you must update it to use standardized Web alternatives:
body property is now a Web ReadableStream instead of a Node.js Readable. To use Node.js stream methods like .pipe(), wrap the body using Readable.fromWeb() from the node:stream module.headers property on APIError objects is now an instance of the Web Headers class. It is no longer a plain Record<string, string | null | undefined>.// Before:
const res = await client.example.retrieve('string/with/slash').asResponse();
res.body.pipe(process.stdout);
// After:
import { Readable } from 'node:stream';
const res = await client.example.retrieve('string/with/slash').asResponse();
Readable.fromWeb(res.body).pipe(process.stdout);The fileFromPath helper has been removed. For file uploads, use native Node.js streams or runtime-specific file APIs (like Bun.file for Bun).
// Before
Cloudflare.fileFromPath('path/to/file');
// After
import fs from 'fs';
fs.createReadStream('path/to/file');Install the library directly from the GitLab repository using npm. Note that once published to npm, you will be able to use npm install cloudflare.
npm install git+ssh://git@gitlab.cfdata.org:cloudflare/sdks/cloudflare-typescript.gitEnsure your development environment meets the following minimum version requirements to use the latest SDK:
When calling methods that do not require a body, query, or header parameters, you can no longer pass the options object as the first argument. You must now explicitly provide null, undefined, or an empty object {} as the first argument to reach the options argument.
Example transition:
- client.example.list({ headers: { ... } });
+ client.example.list({}, { headers: { ... } });
+ client.example.list(null, { headers: { ... } });
+ client.example.list(undefined, { headers: { ... } });client.example.list();
client.example.list({}, { headers: { ... } });
client.example.list(null, { headers: { ... } });
client.example.list(undefined, { headers: { ... } });
- client.example.list({ headers: { ... } });
+ client.example.list({}, { headers: { ... } });The library has been refactored to separate internal and public code. Many modules previously available at the top level have been moved to a core directory. Update your import paths as follows:
// Before
import 'cloudflare/error';
import 'cloudflare/pagination';
import 'cloudflare/resource';
import 'cloudflare/uploads';
// After
import 'cloudflare/core/error';
import 'cloudflare/core/pagination';
import 'cloudflare/core/resource';
import 'cloudflare/core/uploads';If you need to interact with undocumented endpoints or use undocumented parameters, you can bypass the library's type safety:
client.post('/path')). Client options like retries are still respected.// @ts-expect-error to suppress TypeScript errors. For GET requests, extra params are sent as query strings; for other verbs, they are sent in the request body.// @ts-expect-error or by casting to a custom type.Note: The library does not validate or strip extra properties at runtime; they will be sent to the API as-is.
// Undocumented endpoint
await client.post('/some/path', {
body: { some_prop: 'foo' },
query: { some_query_arg: 'bar' },
});
// Undocumented parameter
client.zones.create({
// @ts-expect-error baz is not yet public
baz: 'undocumented option',
});The httpAgent option has been removed because it relied on node:http agents, which are incompatible with the built-in fetch implementation used by modern runtimes. To configure proxies or custom fetch behavior, use the fetchOptions property. If you are in a Node.js environment, use undici.ProxyAgent to provide a dispatcher within fetchOptions.
import Cloudflare from 'cloudflare';
import * as undici from 'undici';
const proxyAgent = new undici.ProxyAgent(process.env.PROXY_URL);
const client = new Cloudflare({
fetchOptions: {
dispatcher: proxyAgent,
},
});