-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: generate Worker file for Pages (#1)
* feat: build single-file worker for pages * debug: wtf * debug: print stripe errors * debug: remove messages
- Loading branch information
Showing
8 changed files
with
78 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ node_modules | |
*-lock.* | ||
*.lock | ||
*.log | ||
|
||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
{ | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"build": "worktop build worker/index.ts" | ||
}, | ||
"dependencies": { | ||
"stripe": "8.186.1" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-types": "3.1.1", | ||
"@esbuild-plugins/node-modules-polyfill": "0.1.2", | ||
"worktop.build": "0.0.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"entryPoint": "/functions", | ||
"baseURL": "/api" | ||
"baseURL": "/api/checkout", | ||
"entryPoint": "/build/index.mjs" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2020", | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"types": ["@cloudflare/workers-types"], | ||
"lib": ["es2020"] | ||
}, | ||
"include": [ | ||
"worker/**/*.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as checkout from './checkout'; | ||
import type { ModuleWorker } from './types'; | ||
|
||
const worker: ModuleWorker = { | ||
fetch(req, env, ctx) { | ||
// POST /api/checkout | ||
if (req.method === 'POST') { | ||
return checkout.create(req, env, ctx); | ||
} | ||
|
||
// GET|HEAD /api/checkout | ||
if (req.method === 'GET' || req.method === 'HEAD') { | ||
return checkout.lookup(req, env, ctx); | ||
} | ||
} | ||
} | ||
|
||
export default worker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// The necessary binding(s) | ||
export interface Bindings { | ||
STRIPE_API_KEY: string; | ||
} | ||
|
||
// The "fetch" handler | ||
export type Handler = ExportedHandlerFetchHandler<Bindings>; | ||
|
||
// Alias for the entire Module Worker definition | ||
export type ModuleWorker = ExportedHandler<Bindings>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { define } from 'worktop.build'; | ||
import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill'; | ||
|
||
// Grab a `STRIPE_API_KEY` value | ||
const { STRIPE_API_KEY } = process.env; | ||
|
||
export default define({ | ||
modify(config) { | ||
config.plugins = config.plugins || []; | ||
config.plugins.push(NodeModulesPolyfillPlugin()); | ||
|
||
config.define = { | ||
'STRIPE_API_KEY': JSON.stringify(STRIPE_API_KEY) | ||
}; | ||
} | ||
}); |