Skip to content

Commit

Permalink
docs: add return type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Cherry committed Apr 14, 2024
1 parent 131686a commit 41dbee9
Show file tree
Hide file tree
Showing 46 changed files with 75 additions and 45 deletions.
30 changes: 30 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
* text=auto
* text eol=lf

# (binary is a macro for -text -diff)
*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.avif binary
*.webp binary
*.jxl binary
*.ico binary
*.mov binary
*.mp4 binary
*.mp3 binary
*.flv binary
*.fla binary
*.swf binary
*.gz binary
*.zip binary
*.7z binary
*.ttf binary
*.eot binary
*.woff binary
*.woff2 binary
*.pyc binary
*.pdf binary
*.ez binary
*.bz2 binary
*.swp binary
2 changes: 1 addition & 1 deletion content/r2/api/s3/presigned-urls.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const r2 = new AwsClient({
});

export default {
async fetch(req: Request) {
async fetch(req: Request): Promise<Response> {
// This is just an example to demonstrating using aws4fetch to generate a presigned URL.
// This Worker should not be used as-is as it does not authenticate the request, meaning
// that anyone can upload to your bucket.
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/103-early-hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const HTML = `
`;

export default {
async fetch(req: Request) {
async fetch(req: Request): Promise<Response> {
// If request is for test.css, serve the raw CSS
if (/test\.css$/.test(req.url)) {
return new Response(CSS, {
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/ab-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default {
const NAME = "myExampleWorkersABTest";

export default {
async fetch(req: Request) {
async fetch(req: Request): Promise<Response> {
const url = new URL(req.url);
// Enable Passthrough to allow direct access to control and test routes.
if (url.pathname.startsWith("/control") || url.pathname.startsWith("/test"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default {

```ts
export default {
async fetch(req: Request) {
async fetch(req: Request): Promise<Response> {
const data =
req.cf !== undefined
? req.cf
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/aggregate-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
/**
* someHost is set up to return JSON responses
* Replace url1 and url2 with the hosts you wish to send requests to
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/alter-headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
const response = await fetch(request);

// Clone the response so that it's no longer immutable
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/auth-with-headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
/**
* @param {string} PRESHARED_AUTH_HEADER_KEY Custom header to check for key
* @param {string} PRESHARED_AUTH_HEADER_VALUE Hard coded key value
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/basic-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ function timingSafeEqual(a: string, b: string) {
}

export default {
async fetch(request: Request, env: { PASSWORD: string }) {
async fetch(request: Request, env: { PASSWORD: string }): Promise<Response> {
const BASIC_USER = "admin";

// You will need an admin password. This should be
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/block-on-tls.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
try {
const tlsVersion = request.cf.tlsVersion;
// Allow only TLS versions 1.2 and 1.3
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/bulk-origin-proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
/**
* An object with different URLs to fetch
* @param {Object} ORIGINS
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/bulk-redirects.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
const externalHostname = "examples.cloudflareworkers.com";

const redirectMap = new Map([
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/cache-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default {

```ts
export default {
async fetch(request: Request, env: unknown, ctx: ExecutionContext) {
async fetch(request: Request, env: unknown, ctx: ExecutionContext): Promise<Response> {
const cacheUrl = new URL(request.url);

// Construct the cache key from the cache URL
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/cache-post-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default {

```ts
export default {
async fetch(request: Request, env: unknown, ctx: ExecutionContext) {
async fetch(request: Request, env: unknown, ctx: ExecutionContext): Promise<Response> {
async function sha256(message) {
// encode as UTF-8
const msgBuffer = await new TextEncoder().encode(message);
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/cache-tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
const requestUrl = new URL(request.url);
const params = requestUrl.searchParams;
const tags =
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/cache-using-fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
let url = new URL(request.url);

if (Math.random() < 0.5) {
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/conditional-response.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
const BLOCKED_HOSTNAMES = ["nope.mywebsite.com", "bye.website.com"];
// Return a new Response based on a URL's hostname
const url = new URL(request.url);
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/cors-header-proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS",
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/country-code-redirect.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
/**
* A map of the URLs to redirect to
* @param {Object} countryMap
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/data-loss-prevention.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
const DEBUG = true;
const SOME_HOOK_SERVER = "https://webhook.flow-wolf.io/hook";

Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/debugging-logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default {

```ts
export default {
async fetch(request: Request, env: unknown, ctx: ExecutionContext) {
async fetch(request: Request, env: unknown, ctx: ExecutionContext): Promise<Response> {
// Service configured to receive logs
const LOG_URL = "https://log-service.example.com/";

Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/extract-cookie-value.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default {
```ts
import { parse } from "cookie";
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
// The name of the cookie
const COOKIE_NAME = "__uid";
const cookie = parse(request.headers.get("Cookie") || "");
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/fetch-html.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ updated: 2024-01-11

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
/**
* Replace `remote` with the host you wish to send requests to
*/
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/fetch-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default {

```ts
export default {
async fetch(request: Request, env: unknown, ctx: ExecutionContext) {
async fetch(request: Request, env: unknown, ctx: ExecutionContext): Promise<Response> {
/**
* Example someHost is set up to take in a JSON request
* Replace url with the host you wish to send requests to
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/geolocation-app-weather.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default {
```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
let endpoint = "https://api.waqi.info/feed/geo:";
const token = ""; //Use a token from https://aqicn.org/api/
let html_style = `body{padding:6em; font-family: sans-serif;} h1{color:#f6821f}`;
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/geolocation-custom-styling.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
let grads = [
[
{ color: "00000c", position: 0 },
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/geolocation-hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
let html_content = "";
let html_style =
"body{padding:6em; font-family: sans-serif;} h1{color:#f6821f;}";
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/hot-link-protection.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
const HOMEPAGE_URL = "https://tutorial.cloudflareworkers.com/";
const PROTECTED_TYPE = "image/";

Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/images-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
// You can find this in the dashboard, it should look something like this: ZWd9g1K7eljCn_KDTu_MWA
const accountHash = "";

Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/logging-headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
console.log(new Map(request.headers));
return new Response("Hello world");
},
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/modify-request-property.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
/**
* Example someHost is set up to return raw JSON
* @param {string} someUrl the URL to send the request to, since we are setting hostname too only path is applied
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/modify-response.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
/**
* @param {string} headerNameSrc Header to get the new value from
* @param {string} headerNameDst Header to set based off of value in src
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/post-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
/**
* Example someHost is set up to take in a JSON request
* Replace url with the host you wish to send requests to
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/read-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
/**
* rawHtmlResponse returns HTML inputted directly
* into the worker script
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/redirect.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
const base = "https://example.com";
const statusCode = 301;

Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/respond-with-another-site.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ layout: example

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
async function MethodNotAllowed(request) {
return new Response(`Method ${request.method} not allowed.`, {
status: 405,
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/return-html.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ updated: 2024-01-11

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
const html = `<!DOCTYPE html>
<body>
<h1>Hello World</h1>
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/return-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ updated: 2024-01-11

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
const data = {
hello: "world",
};
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/rewrite-links.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
const OLD_URL = "developer.mozilla.org";
const NEW_URL = "mynewdomain.com";

Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/security-headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default {

```ts
export default {
async fetch(request: Request) {
async fetch(request: Request): Promise<Response> {
const DEFAULT_SECURITY_HEADERS = {
/*
Secure your application with Content-Security-Policy headers.
Expand Down
2 changes: 1 addition & 1 deletion content/workers/examples/signing-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ const encoder = new TextEncoder();
const EXPIRY = 60;

export default {
async fetch(request: Request, env: { SECRET_DATA: string }) {
async fetch(request: Request, env: { SECRET_DATA: string }): Promise<Response> {
// You will need some secret data to use as a symmetric key. This should be
// attached to your Worker as an encrypted secret.
// Refer to https://developers.cloudflare.com/workers/configuration/secrets/
Expand Down
Loading

0 comments on commit 41dbee9

Please sign in to comment.