From 983c76e08d7732a71c73666e9735f756ad0ba06c Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Tue, 19 Jul 2022 11:42:28 +0800 Subject: [PATCH] copy cloudflare workers type declarations --- serverless/cloudflare/src/cloudflare.d.ts | 148 ++++++++++++++++++++++ serverless/cloudflare/tsconfig.json | 12 ++ 2 files changed, 160 insertions(+) create mode 100644 serverless/cloudflare/src/cloudflare.d.ts create mode 100644 serverless/cloudflare/tsconfig.json diff --git a/serverless/cloudflare/src/cloudflare.d.ts b/serverless/cloudflare/src/cloudflare.d.ts new file mode 100644 index 0000000..5a67c0d --- /dev/null +++ b/serverless/cloudflare/src/cloudflare.d.ts @@ -0,0 +1,148 @@ +// copied from https://github.com/cloudflare/workers-types/blob/master/index.d.ts +// see https://github.com/cloudflare/workers-types/issues/164 + +/** + * An instance of the R2 bucket binding. + */ +interface R2Bucket { + head(key: string): Promise; + get(key: string): Promise; + /** + * Returns R2Object on a failure of the conditional specified in onlyIf. + */ + get( + key: string, + options: R2GetOptions + ): Promise; + get( + key: string, + options?: R2GetOptions + ): Promise; + put( + key: string, + value: + | ReadableStream + | ArrayBuffer + | ArrayBufferView + | string + | null + | Blob, + options?: R2PutOptions + ): Promise; + delete(key: string): Promise; + list(options?: R2ListOptions): Promise; +} + +/** + * Perform the operation conditionally based on meeting the defined criteria. + */ +interface R2Conditional { + etagMatches?: string; + etagDoesNotMatch?: string; + uploadedBefore?: Date; + uploadedAfter?: Date; +} + +/** + * Options for retrieving the object metadata nad payload. + */ +interface R2GetOptions { + onlyIf?: R2Conditional | Headers; + range?: R2Range; +} + +/** + * Metadata that's automatically rendered into R2 HTTP API endpoints. + * ``` + * * contentType -> content-type + * * contentLanguage -> content-language + * etc... + * ``` + * This data is echoed back on GET responses based on what was originally + * assigned to the object (and can typically also be overriden when issuing + * the GET request). + */ +interface R2HTTPMetadata { + contentType?: string; + contentLanguage?: string; + contentDisposition?: string; + contentEncoding?: string; + cacheControl?: string; + cacheExpiry?: Date; +} + +interface R2ListOptions { + limit?: number; + prefix?: string; + cursor?: string; + delimiter?: string; + /** + * If you populate this array, then items returned will include this metadata. + * A tradeoff is that fewer results may be returned depending on how big this + * data is. For now the caps are TBD but expect the total memory usage for a list + * operation may need to be <1MB or even <128kb depending on how many list operations + * you are sending into one bucket. Make sure to look at `truncated` for the result + * rather than having logic like + * ``` + * while (listed.length < limit) { + * listed = myBucket.list({ limit, include: ['customMetadata'] }) + * } + * ``` + */ + include?: ("httpMetadata" | "customMetadata")[]; +} + +/** + * The metadata for the object. + */ +declare abstract class R2Object { + readonly key: string; + readonly version: string; + readonly size: number; + readonly etag: string; + readonly httpEtag: string; + readonly uploaded: Date; + readonly httpMetadata: R2HTTPMetadata; + readonly customMetadata: Record; + writeHttpMetadata(headers: Headers): void; +} + +/** + * The metadata for the object and the body of the payload. + */ +interface R2ObjectBody extends R2Object { + readonly body: ReadableStream; + readonly bodyUsed: boolean; + arrayBuffer(): Promise; + text(): Promise; + json(): Promise; + blob(): Promise; +} + +interface R2Objects { + objects: R2Object[]; + truncated: boolean; + cursor?: string; + delimitedPrefixes: string[]; +} + +interface R2PutOptions { + httpMetadata?: R2HTTPMetadata | Headers; + customMetadata?: Record; + md5?: ArrayBuffer | string; +} + +declare type R2Range = + | { offset: number; length?: number } + | { offset?: number; length: number } + | { suffix: number }; + +interface ReadResult { + value?: any; + done: boolean; +} + +interface ExecutionContext { + waitUntil(promise: Promise): void; + passThroughOnException(): void; +} \ No newline at end of file diff --git a/serverless/cloudflare/tsconfig.json b/serverless/cloudflare/tsconfig.json new file mode 100644 index 0000000..ce445af --- /dev/null +++ b/serverless/cloudflare/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "target": "es6", + "lib": ["es2020", "dom"], + "strict": true, + "moduleResolution": "node", + "paths": { + }, + "types": [] + }, + "include": ["src/*.ts"] +} \ No newline at end of file