### Send GET Request - RestClient (TypeScript) Source: https://github.com/clickup/rest-client/blob/main/docs/classes/RestClient.md Sends a plain GET request to the specified path. Optional arguments are passed as query parameters and automatically encoded using encodeURIComponent. Returns a RestRequest instance. ```TypeScript get(path, args?, accept?): RestRequest ``` -------------------------------- ### Making GET Request with Superstruct Validation (TypeScript) Source: https://github.com/clickup/rest-client/blob/main/README.md This snippet demonstrates how to use the `rest-client` library to make an HTTP GET request in Node.js and validate the JSON response using `superstruct`. It shows client initialization, setting options (timeout, base URL), executing a request with query parameters, enabling debug logging, and applying a `superstruct` schema to the response body. After successful validation, the response object is strongly typed. ```TypeScript import { array, number, object, string } from "superstruct"; import RestClient from "rest-client"; // Initialized once. See RestOptions for lots of other options. const client = new RestClient() .withOptions({ timeoutMs: 1000, logger: (_event) => { /* myLogger.log(event); */ }, }) .withBase("https://reqres.in"); async function main() { // Send individual request using superstruct as a validation backend. const res = await client .get("/api/users", { page: 2 }) .setDebug() .json( object({ total: number(), data: array( object({ id: number(), email: string(), }) ), }) ); console.log("Masked and validated response: ", res); // Notice that `res` is strongly typed! You won't make a typo. console.log("Here is a TS strongly-typed field:", res.data[0].email); } main().catch((e) => console.error(e)); ``` -------------------------------- ### Using RestClient with Superstruct for API Validation (TypeScript) Source: https://github.com/clickup/rest-client/blob/main/docs/README.md This snippet demonstrates how to initialize the RestClient, configure options like timeout and base URL, and perform a GET request. It shows how to use superstruct validators with the .json() method to parse and strongly type the API response, catching validation errors if the response shape is unexpected. It requires superstruct and @clickup/rest-client. ```typescript import { array, number, object, string } from "superstruct"; import RestClient from "rest-client"; // Initialized once. See RestOptions for lots of other options. const client = new RestClient() .withOptions({ timeoutMs: 1000, logger: (_event) => { /* myLogger.log(event); */ }, }) .withBase("https://reqres.in"); async function main() { // Send individual request using superstruct as a validation backend. const res = await client .get("/api/users", { page: 2 }) .setDebug() .json( object({ total: number(), data: array( object({ id: number(), email: string(), }) ), }) ); console.log("Masked and validated response: ", res); // Notice that `res` is strongly typed! You won't make a typo. console.log("Here is a TS strongly-typed field:", res.data[0].email); } main().catch((e) => console.error(e)); ``` -------------------------------- ### Configure Basic Authentication - RestClient (TypeScript) Source: https://github.com/clickup/rest-client/blob/main/docs/classes/RestClient.md Returns a new RestClient instance configured with basic authorization using a username and password token. ```TypeScript withBasic(token): RestClient ``` -------------------------------- ### Depaginating Results with Cursor in TypeScript Source: https://github.com/clickup/rest-client/blob/main/docs/modules.md This function provides an asynchronous generator that repeatedly calls a provided function (`readFunc`) to fetch paginated results. It continues fetching until the `readFunc` returns a null or undefined cursor, yielding items from each page. The `readFunc` must return an array containing the results and the next cursor. ```TypeScript depaginate(readFunc: (cursor: undefined | TCursor) => Promise): AsyncGenerator ``` -------------------------------- ### Adding Request Pacing Middleware in TypeScript Source: https://github.com/clickup/rest-client/blob/main/docs/modules.md This function creates a Rest Client middleware that introduces a delay between requests. It utilizes a `Pacer` implementation or a function that resolves to a `Pacer` to determine the delay. An optional `delayMetric` function can be provided to log or track the applied delays. ```TypeScript paceRequests(pacer: null | Pacer | ((req: RestRequest) => Promise), delayMetric?: (delay: number, reason: string) => void): Middleware ``` -------------------------------- ### Write JSON Body Request - RestClient (TypeScript) Source: https://github.com/clickup/rest-client/blob/main/docs/classes/RestClient.md A shortcut method to send a request with a JSON body. Supports POST, PUT, PATCH, and DELETE methods, defaulting to POST. Returns a RestRequest instance. ```TypeScript writeJson(path, body, method?, accept?): RestRequest ``` -------------------------------- ### Configure OAuth1 Authentication - RestClient (TypeScript) Source: https://github.com/clickup/rest-client/blob/main/docs/classes/RestClient.md Returns a new RestClient instance configured with OAuth1 authentication. It supports token invalidation handling by re-calling the token getter lambda on error for recovery or updates. ```TypeScript withOAuth1(consumer, token): RestClient ``` -------------------------------- ### Send DELETE Request with Body - RestClient (TypeScript) Source: https://github.com/clickup/rest-client/blob/main/docs/classes/RestClient.md A shortcut method to send a DELETE request that can include arguments in the body or query string. Returns a RestRequest instance. ```TypeScript writeDelete(path, args?, accept?): RestRequest ``` -------------------------------- ### Write Raw Body Request - RestClient (TypeScript) Source: https://github.com/clickup/rest-client/blob/main/docs/classes/RestClient.md Writes a raw string, buffer, or stream as the request body with a specified content type. Supports POST, PUT, and PATCH methods, defaulting to POST. Returns a RestRequest instance. ```TypeScript writeRaw(path, body, contentType, method?, accept?): RestRequest ``` -------------------------------- ### Defining Middleware Function Signature TypeScript Source: https://github.com/clickup/rest-client/blob/main/docs/interfaces/Middleware.md This snippet defines the type signature for a callable middleware function. It accepts the current RestRequest and a 'next' function to continue the request chain, returning a Promise resolving to a RestResponse. This signature is required for functions intended to act as middleware in the client. ```typescript (req: RestRequest, next: (req: RestRequest) => Promise): Promise ``` -------------------------------- ### Write Form-Urlencoded Body Request - RestClient (TypeScript) Source: https://github.com/clickup/rest-client/blob/main/docs/classes/RestClient.md A shortcut method to send a request with an 'application/x-www-form-urlencoded' body. Supports POST, PUT, and PATCH methods, defaulting to POST. Returns a RestRequest instance. ```TypeScript writeForm(path, body, method?, accept?): RestRequest ``` -------------------------------- ### Constructing RestError Instance (TypeScript) Source: https://github.com/clickup/rest-client/blob/main/docs/classes/RestError.md Initializes a new instance of the `RestError` class. It accepts a single string parameter, `message`, which provides a human-readable description of the error. This constructor overrides the default `Error` constructor. ```typescript new RestError(message) ``` -------------------------------- ### Determine REST Response Success (TypeScript) Source: https://github.com/clickup/rest-client/blob/main/docs/interfaces/RestOptions.md This callback function determines if a given RestResponse should be considered successful. It allows overriding standard HTTP status code checks. Returns "SUCCESS" to treat as success, "THROW" to treat as an error, or "BEST_EFFORT" to use built-in heuristics. ```TypeScript (res: RestResponse) => "SUCCESS" | "THROW" | "BEST_EFFORT" ``` -------------------------------- ### Identify REST Rate Limit Error (TypeScript) Source: https://github.com/clickup/rest-client/blob/main/docs/interfaces/RestOptions.md This callback determines if a RestResponse indicates a rate limit error. It can return a number for retry delay, "RATE_LIMIT" to indicate a rate limit error (always retried), "SOMETHING_ELSE" if it's not a rate limit, or "BEST_EFFORT" to use built-in logic (like checking Retry-After header). ```TypeScript (res: RestResponse) => number | "BEST_EFFORT" | "SOMETHING_ELSE" | "RATE_LIMIT" ``` -------------------------------- ### RestResponseError Class Definition in TypeScript Source: https://github.com/clickup/rest-client/blob/main/docs/classes/RestResponseError.md Defines the RestResponseError class, inheriting from RestError. It includes properties to store the associated response object and details about the request/response, and a constructor signature for initialization. ```TypeScript // Class: RestResponseError // Hierarchy: RestError -> RestResponseError class RestResponseError extends RestError { // Properties readonly res: RestResponse; readonly method: string; readonly host: string; readonly pathname: string; readonly requestArgs: string; readonly requestBody: string; readonly responseHeaders: string; // Constructors constructor(message: string, res: RestResponse): RestResponseError; } ``` -------------------------------- ### Check for REST Token Invalid Error (TypeScript) Source: https://github.com/clickup/rest-client/blob/main/docs/interfaces/RestOptions.md This callback checks if a RestResponse signifies an invalid authentication token error. It returns a boolean: true if the token is invalid, false otherwise. ```TypeScript (res: RestResponse) => boolean ``` -------------------------------- ### Determine if REST Error is Retriable (TypeScript) Source: https://github.com/clickup/rest-client/blob/main/docs/interfaces/RestOptions.md Called only if the error is not classified as a rate limit error. This callback determines if a RestResponse error is retriable. It can return a number for retry delay, "RETRY" to retry, "NEVER_RETRY" to prevent retries, or "BEST_EFFORT" to use built-in heuristics (e.g., not retrying 404s). ```TypeScript (res: RestResponse, _error: any) => number | "BEST_EFFORT" | "NEVER_RETRY" | "RETRY" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.