@upstash/ratelimit 0.4.4 → 0.4.5-canary.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/.github/actions/redis/action.yaml +58 -0
  2. package/.github/img/dashboard.png +0 -0
  3. package/.github/workflows/release.yml +46 -0
  4. package/.github/workflows/stale.yaml +31 -0
  5. package/.github/workflows/tests.yaml +79 -0
  6. package/README.md +9 -2
  7. package/biome.json +37 -0
  8. package/bun.lockb +0 -0
  9. package/cmd/set-version.js +14 -0
  10. package/examples/cloudflare-workers/package.json +18 -0
  11. package/examples/cloudflare-workers/src/index.ts +35 -0
  12. package/examples/cloudflare-workers/tsconfig.json +105 -0
  13. package/examples/cloudflare-workers/wrangler.toml +3 -0
  14. package/examples/nextjs/LICENSE +21 -0
  15. package/examples/nextjs/README.md +17 -0
  16. package/examples/nextjs/components/Breadcrumb.tsx +67 -0
  17. package/examples/nextjs/components/Header.tsx +18 -0
  18. package/examples/nextjs/components/ReadBlogPost.tsx +9 -0
  19. package/examples/nextjs/components/StarButton.tsx +27 -0
  20. package/examples/nextjs/middleware.ts +35 -0
  21. package/examples/nextjs/next-env.d.ts +5 -0
  22. package/examples/nextjs/package.json +27 -0
  23. package/examples/nextjs/pages/_app.tsx +47 -0
  24. package/examples/nextjs/pages/api/blocked.ts +6 -0
  25. package/examples/nextjs/pages/api/hello.ts +5 -0
  26. package/examples/nextjs/pages/index.tsx +62 -0
  27. package/examples/nextjs/postcss.config.js +6 -0
  28. package/examples/nextjs/public/favicon.ico +0 -0
  29. package/examples/nextjs/public/github.svg +11 -0
  30. package/examples/nextjs/public/upstash.svg +27 -0
  31. package/examples/nextjs/styles/globals.css +76 -0
  32. package/examples/nextjs/tailwind.config.js +19 -0
  33. package/examples/nextjs/tsconfig.json +21 -0
  34. package/examples/nextjs13/README.md +38 -0
  35. package/examples/nextjs13/app/favicon.ico +0 -0
  36. package/examples/nextjs13/app/globals.css +107 -0
  37. package/examples/nextjs13/app/layout.tsx +18 -0
  38. package/examples/nextjs13/app/page.module.css +271 -0
  39. package/examples/nextjs13/app/route.tsx +14 -0
  40. package/examples/nextjs13/next.config.js +8 -0
  41. package/examples/nextjs13/package.json +22 -0
  42. package/examples/nextjs13/public/next.svg +1 -0
  43. package/examples/nextjs13/public/thirteen.svg +1 -0
  44. package/examples/nextjs13/public/vercel.svg +1 -0
  45. package/examples/nextjs13/tsconfig.json +28 -0
  46. package/examples/remix/.env.example +2 -0
  47. package/examples/remix/.eslintrc.js +4 -0
  48. package/examples/remix/README.md +59 -0
  49. package/examples/remix/app/root.tsx +25 -0
  50. package/examples/remix/app/routes/index.tsx +47 -0
  51. package/examples/remix/package.json +32 -0
  52. package/examples/remix/public/favicon.ico +0 -0
  53. package/examples/remix/remix.config.js +12 -0
  54. package/examples/remix/remix.env.d.ts +2 -0
  55. package/examples/remix/server.js +4 -0
  56. package/examples/remix/tsconfig.json +22 -0
  57. package/examples/with-vercel-kv/README.md +51 -0
  58. package/examples/with-vercel-kv/app/favicon.ico +0 -0
  59. package/examples/with-vercel-kv/app/globals.css +27 -0
  60. package/examples/with-vercel-kv/app/layout.tsx +21 -0
  61. package/examples/with-vercel-kv/app/page.tsx +71 -0
  62. package/examples/with-vercel-kv/next.config.js +8 -0
  63. package/examples/with-vercel-kv/package.json +25 -0
  64. package/examples/with-vercel-kv/postcss.config.js +6 -0
  65. package/examples/with-vercel-kv/public/next.svg +1 -0
  66. package/examples/with-vercel-kv/public/vercel.svg +1 -0
  67. package/examples/with-vercel-kv/tailwind.config.js +17 -0
  68. package/examples/with-vercel-kv/tsconfig.json +28 -0
  69. package/package.json +12 -33
  70. package/src/analytics.test.ts +23 -0
  71. package/src/analytics.ts +92 -0
  72. package/src/blockUntilReady.test.ts +56 -0
  73. package/src/cache.test.ts +41 -0
  74. package/src/cache.ts +43 -0
  75. package/src/duration.test.ts +23 -0
  76. package/src/duration.ts +30 -0
  77. package/src/index.ts +17 -0
  78. package/src/multi.ts +365 -0
  79. package/src/ratelimit.test.ts +155 -0
  80. package/src/ratelimit.ts +238 -0
  81. package/src/single.ts +487 -0
  82. package/src/test_utils.ts +65 -0
  83. package/src/tools/seed.ts +37 -0
  84. package/src/types.ts +78 -0
  85. package/src/version.ts +1 -0
  86. package/tsconfig.json +103 -0
  87. package/tsup.config.js +11 -0
  88. package/turbo.json +16 -0
  89. package/dist/index.d.ts +0 -556
  90. package/dist/index.js +0 -832
  91. package/dist/index.js.map +0 -1
  92. package/dist/index.mjs +0 -803
  93. package/dist/index.mjs.map +0 -1
@@ -0,0 +1,58 @@
1
+ name: Create local redis server
2
+
3
+ inputs:
4
+ UPSTASH_REDIS_REST_URL:
5
+ required: true
6
+ UPSTASH_REDIS_REST_TOKEN:
7
+ required: true
8
+ UPSTASH_REPO_ACCESS_TOKEN:
9
+ required: true
10
+ REDIS_SERVER_CONFIG:
11
+ required: true
12
+
13
+ runs:
14
+ using: "composite"
15
+
16
+ steps:
17
+ - name: Check out Redis Server
18
+ uses: actions/checkout@v2
19
+ with:
20
+ repository: upstash/redis-server
21
+ token: ${{ inputs.UPSTASH_REPO_ACCESS_TOKEN }}
22
+ path: redis-server
23
+
24
+ - uses: actions/setup-go@v2
25
+ with:
26
+ stable: "true"
27
+ go-version: "^1.17"
28
+
29
+ - uses: actions/cache@v2
30
+ with:
31
+ path: |
32
+ ~/go/pkg/mod
33
+ ~/.cache/go-build
34
+ key: ${{ runner.os }}-redis-server-${{ hashFiles('**/go.mod') }}
35
+ restore-keys: |
36
+ ${{ runner.os }}-redis-server
37
+
38
+ - run: |
39
+ echo "$REDIS_SERVER_CONFIG" >> config.json
40
+ shell: bash
41
+ env:
42
+ REDIS_SERVER_CONFIG: ${{ inputs.REDIS_SERVER_CONFIG }}
43
+
44
+ - run: |
45
+ make
46
+ ./upstash-redis -log-level error -config $GITHUB_WORKSPACE/config.json &
47
+ working-directory: ./redis-server/cmd
48
+ shell: bash
49
+ env:
50
+ UPSTASH_REDIS_REST_URL: ${{ inputs.UPSTASH_REDIS_REST_URL }}
51
+ UPSTASH_REDIS_REST_TOKEN: ${{ inputs.UPSTASH_REDIS_REST_TOKEN }}
52
+
53
+ - run: |
54
+ curl $UPSTASH_REDIS_REST_URL/info -H "Authorization: Bearer $UPSTASH_REDIS_REST_TOKEN"
55
+ shell: bash
56
+ env:
57
+ UPSTASH_REDIS_REST_URL: ${{ inputs.UPSTASH_REDIS_REST_URL }}
58
+ UPSTASH_REDIS_REST_TOKEN: ${{ inputs.UPSTASH_REDIS_REST_TOKEN }}
Binary file
@@ -0,0 +1,46 @@
1
+ name: Release
2
+
3
+ on:
4
+ release:
5
+ types:
6
+ - published
7
+
8
+ jobs:
9
+ release:
10
+ name: Release
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - name: Checkout Repo
14
+ uses: actions/checkout@v2
15
+
16
+
17
+ - name: Setup Node
18
+ uses: actions/setup-node@v2
19
+ with:
20
+ node-version: 18
21
+
22
+ - name: Set version
23
+ run: node cmd/set-version.js . ${GITHUB_REF#refs/*/}
24
+
25
+ - name: Install bun
26
+ run: npm install -g bun
27
+
28
+ - name: Install dependencies
29
+ run: bun install
30
+
31
+ - name: Build
32
+ run: bun run build
33
+
34
+ - name: Add npm token
35
+ run: echo "//registry.npmjs.org/:_authToken=${{secrets.NPM_TOKEN}}" > .npmrc
36
+
37
+
38
+ - name: Publish release candidate
39
+ if: "github.event.release.prerelease"
40
+ run: npm publish --access public --tag=canary --no-git-checks
41
+
42
+ - name: Publish
43
+ if: "!github.event.release.prerelease"
44
+ run: npm publish --access public --no-git-checks
45
+
46
+
@@ -0,0 +1,31 @@
1
+ name: 'Close stale issues and PRs'
2
+ on:
3
+ schedule:
4
+ - cron: "30 1 * * *"
5
+
6
+ env:
7
+ DAYS_BEFORE_ISSUE_STALE: 30
8
+ DAYS_BEFORE_ISSUE_CLOSE: 7
9
+ DAYS_BEFORE_PR_STALE: 30
10
+ DAYS_BEFORE_PR_CLOSE: 7
11
+
12
+ jobs:
13
+ close-issues:
14
+ runs-on: ubuntu-latest
15
+ permissions:
16
+ issues: write
17
+ pull-requests: write
18
+ steps:
19
+ - uses: actions/stale@v8
20
+ with:
21
+ days-before-issue-stale: ${{ env.DAYS_BEFORE_ISSUE_STALE }}
22
+ days-before-issue-close: ${{ env.DAYS_BEFORE_ISSUE_CLOSE }}
23
+ days-before-pr-stale: ${{ env.DAYS_BEFORE_PR_STALE }}
24
+ days-before-pr-close: ${{ env.DAYS_BEFORE_PR_CLOSE }}
25
+ stale-issue-label: "Inactive Issue"
26
+ stale-issue-message: 'This issue is stale because it has been open ${{ env.DAYS_BEFORE_ISSUE_STALE }} days with no activity. Remove stale label or comment or this will be closed in ${{ env.DAYS_BEFORE_ISSUE_CLOSE }} days.'
27
+ stale-pr-message: 'This PR is stale because it has been open ${{ env.DAYS_BEFORE_PR_STALE }} days with no activity. Remove stale label or comment or this will be closed in ${{ env.DAYS_BEFORE_PR_CLOSE }} days.'
28
+ close-issue-message: 'This issue was closed because it has been stalled for ${{ env.DAYS_BEFORE_ISSUE_CLOSE }} days with no activity.'
29
+ close-pr-message: 'This PR was closed because it has been stalled for ${{ env.DAYS_BEFORE_PR_CLOSE }} days with no activity.'
30
+ exempt-issue-labels: "Active Issue"
31
+ repo-token: ${{ secrets.GITHUB_TOKEN }}
@@ -0,0 +1,79 @@
1
+ name: Tests
2
+ on:
3
+ pull_request:
4
+ schedule:
5
+ - cron: "0 0 * * *" # daily
6
+
7
+ jobs:
8
+ build:
9
+ runs-on: ubuntu-latest
10
+
11
+ name: Build
12
+ steps:
13
+ - name: Setup repo
14
+ uses: actions/checkout@v2
15
+
16
+ - uses: actions/setup-node@v3
17
+ with:
18
+ node-version: 18
19
+
20
+ - name: Install bun
21
+ run: npm install -g bun
22
+
23
+ - name: Install dependencies
24
+ run: bun install
25
+
26
+ - name: Verify formatting
27
+ run: bun run fmt
28
+
29
+ - name: Start redis server
30
+ uses: ./.github/actions/redis
31
+ with:
32
+ UPSTASH_REDIS_REST_URL: http://127.0.0.1:6379
33
+ UPSTASH_REDIS_REST_TOKEN: ${{ secrets.UPSTASH_REDIS_REST_TOKEN }}
34
+ UPSTASH_REPO_ACCESS_TOKEN: ${{ secrets.UPSTASH_REPO_ACCESS_TOKEN }}
35
+ REDIS_SERVER_CONFIG: ${{ secrets.REDIS_SERVER_CONFIG }}
36
+
37
+ - name: Build
38
+ run: bun run build
39
+ env:
40
+ UPSTASH_REDIS_REST_URL: http://127.0.0.1:6379
41
+ UPSTASH_REDIS_REST_TOKEN: ${{ secrets.UPSTASH_REDIS_REST_TOKEN }}
42
+
43
+
44
+ test:
45
+ runs-on: ubuntu-latest
46
+ name: Tests
47
+ steps:
48
+ - name: Setup repo
49
+ uses: actions/checkout@v2
50
+
51
+ - uses: actions/setup-node@v3
52
+ with:
53
+ node-version: 18
54
+
55
+ - name: Install bun
56
+ run: npm install -g bun
57
+
58
+ - name: Install dependencies
59
+ run: bun install
60
+
61
+ - name: Start redis server
62
+ uses: ./.github/actions/redis
63
+ with:
64
+ UPSTASH_REDIS_REST_URL: http://127.0.0.1:6379
65
+ UPSTASH_REDIS_REST_TOKEN: ${{ secrets.UPSTASH_REDIS_REST_TOKEN }}
66
+ UPSTASH_REPO_ACCESS_TOKEN: ${{ secrets.UPSTASH_REPO_ACCESS_TOKEN }}
67
+ REDIS_SERVER_CONFIG: ${{ secrets.REDIS_SERVER_CONFIG }}
68
+
69
+ - name: Run tests
70
+ run: bun run test
71
+ env:
72
+ UPSTASH_REDIS_REST_URL: http://127.0.0.1:6379
73
+ UPSTASH_REDIS_REST_TOKEN: ${{ secrets.UPSTASH_REDIS_REST_TOKEN }}
74
+ EU2_UPSTASH_REDIS_REST_URL: ${{ secrets.EU2_UPSTASH_REDIS_REST_URL }}
75
+ EU2_UPSTASH_REDIS_REST_TOKEN: ${{ secrets.EU2_UPSTASH_REDIS_REST_TOKEN }}
76
+ APN_UPSTASH_REDIS_REST_URL: ${{ secrets.APN_UPSTASH_REDIS_REST_URL }}
77
+ APN_UPSTASH_REDIS_REST_TOKEN: ${{ secrets.APN_UPSTASH_REDIS_REST_TOKEN }}
78
+ US1_UPSTASH_REDIS_REST_URL: ${{ secrets.US1_UPSTASH_REDIS_REST_URL }}
79
+ US1_UPSTASH_REDIS_REST_TOKEN: ${{ secrets.US1_UPSTASH_REDIS_REST_TOKEN }}
package/README.md CHANGED
@@ -7,7 +7,7 @@ It is the only connectionless (HTTP based) rate limiting library and designed
7
7
  for:
8
8
 
9
9
  - Serverless functions (AWS Lambda, Vercel ...)
10
- - Cloudflare Workers
10
+ - Cloudflare Workers & Pages
11
11
  - Vercel Edge
12
12
  - Fastly Compute@Edge
13
13
  - Next.js, Jamstack ...
@@ -80,7 +80,7 @@ documentation on how to create a redis instance.
80
80
 
81
81
  ```ts
82
82
  import { Ratelimit } from "@upstash/ratelimit"; // for deno: see above
83
- import { Redis } from "@upstash/redis";
83
+ import { Redis } from "@upstash/redis"; // see below for cloudflare and fastly adapters
84
84
 
85
85
  // Create a new ratelimiter, that allows 10 requests per 10 seconds
86
86
  const ratelimit = new Ratelimit({
@@ -107,6 +107,13 @@ doExpensiveCalculation();
107
107
  return "Here you go!";
108
108
  ```
109
109
 
110
+ For Cloudflare Workers and Fastly Compute@Edge, you can use the following imports:
111
+
112
+ ```ts
113
+ import { Redis } from "@upstash/redis/cloudflare"; // for cloudflare workers and pages
114
+ import { Redis } from "@upstash/redis/fastly"; // for fastly compute@edge
115
+ ```
116
+
110
117
  [Here's a complete nextjs example](https://github.com/upstash/ratelimit/tree/main/examples/nextjs)
111
118
 
112
119
  The `limit` method returns some more metadata that might be useful to you:
package/biome.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "$schema": "https://biomejs.dev/schemas/1.0.0/schema.json",
3
+
4
+ "linter": {
5
+ "enabled": true,
6
+ "rules": {
7
+ "recommended": true,
8
+ "a11y": {
9
+ "noSvgWithoutTitle": "off"
10
+ },
11
+ "correctness": {
12
+ "noUnusedVariables": "warn"
13
+ },
14
+ "security": {
15
+ "noDangerouslySetInnerHtml": "off"
16
+ },
17
+ "style": {
18
+ "useBlockStatements": "error",
19
+ "noNonNullAssertion": "off"
20
+ },
21
+ "performance": {
22
+ "noDelete": "off"
23
+ },
24
+ "suspicious": {
25
+ "noExplicitAny": "off"
26
+ }
27
+ },
28
+ "ignore": ["node_modules", ".next", "dist", ".nuxt", ".contentlayer"]
29
+ },
30
+ "formatter": {
31
+ "indentStyle": "space",
32
+ "indentWidth": 2,
33
+ "enabled": true,
34
+ "lineWidth": 100,
35
+ "ignore": ["node_modules", ".next", "dist", ".nuxt", ".contentlayer"]
36
+ }
37
+ }
package/bun.lockb ADDED
Binary file
@@ -0,0 +1,14 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+
4
+ const root = process.argv[2]; // path to project root
5
+ const version = process.argv[3].replace(/^v/, ""); // new version
6
+
7
+ console.log(`Updating version=${version} in ${root}`);
8
+
9
+ const content = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf-8"));
10
+
11
+ content.version = version;
12
+
13
+ fs.writeFileSync(path.join(root, "package.json"), JSON.stringify(content, null, 2));
14
+ fs.writeFileSync(path.join(root, "src", "version.ts"), `export const VERSION = "${version}";`);
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "cloudflare-workers",
3
+ "version": "0.0.0",
4
+ "devDependencies": {
5
+ "@cloudflare/workers-types": "^4.20230419.0",
6
+ "typescript": "^5.0.4",
7
+ "wrangler": "2.20.0"
8
+ },
9
+ "private": true,
10
+ "scripts": {
11
+ "dev": "wrangler dev",
12
+ "deploy": "wrangler publish"
13
+ },
14
+ "dependencies": {
15
+ "@upstash/ratelimit": "link:../../dist",
16
+ "@upstash/redis": "^1.20.6"
17
+ }
18
+ }
@@ -0,0 +1,35 @@
1
+ import { Ratelimit } from "@upstash/ratelimit";
2
+ import { Redis } from "@upstash/redis/cloudflare";
3
+ export interface Env {
4
+ UPSTASH_REDIS_REST_URL: string;
5
+ UPSTASH_REDIS_REST_TOKEN: string;
6
+ }
7
+
8
+ const cache = new Map();
9
+
10
+ export default {
11
+ async fetch(request: Request, env: Env, _ctx: ExecutionContext): Promise<Response> {
12
+ try {
13
+ console.log("URL:", env.UPSTASH_REDIS_REST_URL);
14
+
15
+ if (new URL(request.url).pathname !== "/limit") {
16
+ return new Response("go to /limit", { status: 400 });
17
+ }
18
+
19
+ const ratelimit = new Ratelimit({
20
+ redis: Redis.fromEnv(env),
21
+ limiter: Ratelimit.cachedFixedWindow(5, "5 s"),
22
+ ephemeralCache: cache,
23
+ });
24
+
25
+ const res = await ratelimit.limit("identifier");
26
+ if (res.success) {
27
+ return new Response(JSON.stringify(res, null, 2), { status: 200 });
28
+ } else {
29
+ return new Response(JSON.stringify({ res }, null, 2), { status: 429 });
30
+ }
31
+ } catch (err) {
32
+ return new Response((err as Error).message, { status: 500 });
33
+ }
34
+ },
35
+ };
@@ -0,0 +1,105 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Visit https://aka.ms/tsconfig.json to read more about this file */
4
+
5
+ /* Projects */
6
+ // "incremental": true, /* Enable incremental compilation */
7
+ // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
8
+ // "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
9
+ // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
10
+ // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
11
+ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
12
+
13
+ /* Language and Environment */
14
+ "target": "es2021", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
15
+ "lib": [
16
+ "es2021"
17
+ ], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
18
+ "jsx": "react", /* Specify what JSX code is generated. */
19
+ // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
20
+ // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
21
+ // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
22
+ // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
23
+ // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
24
+ // "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
25
+ // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
26
+ // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
27
+
28
+ /* Modules */
29
+ "module": "es2022", /* Specify what module code is generated. */
30
+ // "rootDir": "./", /* Specify the root folder within your source files. */
31
+ "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
32
+ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
33
+ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
34
+ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
35
+ // "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
36
+ "types": [
37
+ "@cloudflare/workers-types"
38
+ ], /* Specify type package names to be included without being referenced in a source file. */
39
+ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
40
+ "resolveJsonModule": true, /* Enable importing .json files */
41
+ // "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
42
+
43
+ /* JavaScript Support */
44
+ "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
45
+ "checkJs": false, /* Enable error reporting in type-checked JavaScript files. */
46
+ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
47
+
48
+ /* Emit */
49
+ // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
50
+ // "declarationMap": true, /* Create sourcemaps for d.ts files. */
51
+ // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
52
+ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */
53
+ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
54
+ // "outDir": "./", /* Specify an output folder for all emitted files. */
55
+ // "removeComments": true, /* Disable emitting comments. */
56
+ "noEmit": true, /* Disable emitting files from a compilation. */
57
+ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
58
+ // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
59
+ // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
60
+ // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
61
+ // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
62
+ // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
63
+ // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
64
+ // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
65
+ // "newLine": "crlf", /* Set the newline character for emitting files. */
66
+ // "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
67
+ // "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
68
+ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
69
+ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
70
+ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */
71
+ // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
72
+
73
+ /* Interop Constraints */
74
+ "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
75
+ "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
76
+ // "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
77
+ // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
78
+ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
79
+
80
+ /* Type Checking */
81
+ "strict": true, /* Enable all strict type-checking options. */
82
+ // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
83
+ // "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
84
+ // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
85
+ // "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
86
+ // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
87
+ // "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
88
+ // "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
89
+ // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
90
+ // "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
91
+ // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
92
+ // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
93
+ // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
94
+ // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
95
+ // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
96
+ // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
97
+ // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
98
+ // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
99
+ // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
100
+
101
+ /* Completeness */
102
+ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
103
+ "skipLibCheck": true /* Skip type checking all .d.ts files. */
104
+ }
105
+ }
@@ -0,0 +1,3 @@
1
+ name = "cloudflare-workers"
2
+ main = "src/index.ts"
3
+ compatibility_date = "2022-08-11"
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Upstash
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,17 @@
1
+ # Next.js + Tailwind CSS Example
2
+
3
+ This example shows how to use [Tailwind CSS](https://tailwindcss.com/)
4
+ [(v3.0)](https://tailwindcss.com/blog/tailwindcss-v3) with Next.js. It follows
5
+ the steps outlined in the official
6
+ [Tailwind docs](https://tailwindcss.com/docs/guides/nextjs).
7
+
8
+ ## How to use
9
+
10
+ Execute
11
+ [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app)
12
+ with [npm](https://docs.npmjs.com/cli/init) or
13
+ [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
14
+
15
+ ```bash
16
+ yarn create next-app -e https://github.com/upstash/next-template your-app-name
17
+ ```
@@ -0,0 +1,67 @@
1
+ import Image from "next/image";
2
+ import React from "react";
3
+
4
+ export type BreadcrumbItemProps = {
5
+ name: string;
6
+ url: string;
7
+ };
8
+
9
+ export type BreadcrumbProps = {
10
+ data: BreadcrumbItemProps[];
11
+ showRoot?: boolean;
12
+ };
13
+
14
+ export function BreadcrumbDivider() {
15
+ return <span className="text-gray-300">/</span>;
16
+ }
17
+
18
+ export function BreadcrumbItem({ url, name }: BreadcrumbItemProps) {
19
+ return (
20
+ <a
21
+ className="cursor-pointer hover:underline"
22
+ target="_blank"
23
+ rel="noopener noreferrer"
24
+ href={url}
25
+ >
26
+ {name}
27
+ </a>
28
+ );
29
+ }
30
+
31
+ export function Breadcrumb({ data, showRoot = true }: BreadcrumbProps) {
32
+ return (
33
+ <div className="flex items-center gap-1.5">
34
+ <svg width="20" height="20" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
35
+ <path
36
+ fill="currentColor"
37
+ fillRule="evenodd"
38
+ d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z"
39
+ />
40
+ </svg>
41
+
42
+ {showRoot && (
43
+ <React.Fragment>
44
+ <span className="text-gray-300">/</span>
45
+
46
+ <a
47
+ className="cursor-pointer hover:underline"
48
+ href="https://github.com/upstash"
49
+ target="_blank"
50
+ rel="noopener noreferrer"
51
+ >
52
+ upstash
53
+ </a>
54
+ </React.Fragment>
55
+ )}
56
+
57
+ {data.map((item) => {
58
+ return (
59
+ <React.Fragment key={item.url}>
60
+ <BreadcrumbDivider />
61
+ <BreadcrumbItem url={item.url} name={item.name} />
62
+ </React.Fragment>
63
+ );
64
+ })}
65
+ </div>
66
+ );
67
+ }
@@ -0,0 +1,18 @@
1
+ import React from "react";
2
+ import { Breadcrumb, BreadcrumbProps } from "./Breadcrumb";
3
+ import StarButton from "./StarButton";
4
+
5
+ export type HeaderProps = {
6
+ breadcrumbOptions: BreadcrumbProps;
7
+ };
8
+
9
+ export default function Header({ breadcrumbOptions }: HeaderProps) {
10
+ return (
11
+ <header className="relative z-10 flex items-center bg-gray-900 py-4 px-6 text-gray-50">
12
+ <Breadcrumb {...breadcrumbOptions} />
13
+ <div className="ml-auto hidden sm:block">
14
+ <StarButton {...[...breadcrumbOptions?.data].pop()} />
15
+ </div>
16
+ </header>
17
+ );
18
+ }
@@ -0,0 +1,9 @@
1
+ import React from "react";
2
+
3
+ export default function ReadBlogPost({
4
+ children,
5
+ }: {
6
+ children: React.ReactNode;
7
+ }) {
8
+ return <div className="bg-gray-100 p-4 text-center">{children}</div>;
9
+ }
@@ -0,0 +1,27 @@
1
+ export type StarButtonProps = {
2
+ url?: string;
3
+ };
4
+
5
+ export default function StarButton({ url }: StarButtonProps) {
6
+ if (!url) {
7
+ return null;
8
+ }
9
+
10
+ return (
11
+ <a
12
+ href={url}
13
+ target="_blank"
14
+ rel="noopener noreferrer"
15
+ className="flex cursor-pointer items-center gap-1.5 rounded bg-white px-4 py-1.5 text-gray-700 hover:bg-primary-50 hover:text-primary-700"
16
+ >
17
+ <svg width="18" height="18" viewBox="0 0 16 16" version="1.1">
18
+ <path
19
+ fill="currentColor"
20
+ fillRule="evenodd"
21
+ d="M8 .25a.75.75 0 01.673.418l1.882 3.815 4.21.612a.75.75 0 01.416 1.279l-3.046 2.97.719 4.192a.75.75 0 01-1.088.791L8 12.347l-3.766 1.98a.75.75 0 01-1.088-.79l.72-4.194L.818 6.374a.75.75 0 01.416-1.28l4.21-.611L7.327.668A.75.75 0 018 .25z"
22
+ />
23
+ </svg>
24
+ <span>Star on GitHub</span>
25
+ </a>
26
+ );
27
+ }