Skip to content

Commit

Permalink
chore: generate Worker file for Pages (#1)
Browse files Browse the repository at this point in the history
* feat: build single-file worker for pages

* debug: wtf

* debug: print stripe errors

* debug: remove messages
  • Loading branch information
lukeed committed Nov 6, 2021
1 parent 22a0f79 commit 93fc8c7
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ node_modules
*-lock.*
*.lock
*.log

/build
9 changes: 9 additions & 0 deletions package.json
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"
}
}
4 changes: 2 additions & 2 deletions pages-functions-beta.json
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"
}
12 changes: 12 additions & 0 deletions tsconfig.json
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"
]
}
13 changes: 9 additions & 4 deletions functions/checkout.js → worker/checkout.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import Stripe from 'stripe';
import type { Handler } from './types';

// Injected during `build` script
declare const STRIPE_API_KEY: string;

const stripe = new Stripe(STRIPE_API_KEY, {
// Cloudflare Workers use the Fetch API for their API requests.
httpClient: Stripe.createFetchHttpClient()
httpClient: Stripe.createFetchHttpClient(),
apiVersion: '2020-08-27',
});

function reply(message, status) {
function reply(message: string, status: number): Response {
return new Response(message, { status });
}

/**
* POST /api/checkout
*/
export async function onRequestPost({ request }) {
export const create: Handler = async function (request) {
// Accomodates preview deployments AND custom domains
// @example "https://<hash>.<branch>.<project>.pages.dev"
const { origin } = new URL(request.url);
Expand Down Expand Up @@ -51,7 +56,7 @@ export async function onRequestPost({ request }) {
/**
* GET /api/checkout?sessionid=XYZ
*/
export async function onRequestGet({ request }) {
export const lookup: Handler = async function (request) {
const { searchParams } = new URL(request.url);

const ident = searchParams.get('sessionid');
Expand Down
18 changes: 18 additions & 0 deletions worker/index.ts
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;
10 changes: 10 additions & 0 deletions worker/types.ts
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>;
16 changes: 16 additions & 0 deletions worktop.config.js
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)
};
}
});

0 comments on commit 93fc8c7

Please sign in to comment.