Skip to content

Commit c9cac06

Browse files
committed
chore: update .prettierrc to use double quotes instead of single quotes
1 parent 92195d7 commit c9cac06

8 files changed

Lines changed: 68 additions & 68 deletions

File tree

.github/dependabot.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
version: 2
22
updates:
3-
- package-ecosystem: 'npm'
4-
directory: '/'
3+
- package-ecosystem: "npm"
4+
directory: "/"
55
schedule:
6-
interval: 'daily'
7-
- package-ecosystem: 'github-actions'
8-
directory: '/'
6+
interval: "daily"
7+
- package-ecosystem: "github-actions"
8+
directory: "/"
99
schedule:
10-
interval: 'daily'
10+
interval: "daily"

.prettierrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"printWidth": 140,
3-
"singleQuote": true,
3+
"singleQuote": false,
44
"semi": true,
55
"useTabs": false,
66
"tabWidth": 2

src/_index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { router } from './router';
1+
import { router } from "./router";
22

33
export default {
44
async fetch(request, env, ctx): Promise<Response> {

src/router.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
1-
import crypto from 'crypto';
1+
import crypto from "crypto";
22

33
export async function router(request: Request, env: Env, ctx: ExecutionContext) {
4-
if (request.method !== 'POST') {
5-
return new Response('Method Not Allowed', { status: 405 });
4+
if (request.method !== "POST") {
5+
return new Response("Method Not Allowed", { status: 405 });
66
}
77

88
//////////////////////////////////////////////
99
//此段應該在 Zeabur 修復 Bug 後移除
10-
console.log(request.headers.get('X-ZSend-Event'));
11-
if (request.headers.get('X-ZSend-Event') === 'test') {
12-
return new Response('OK!');
10+
console.log(request.headers.get("X-ZSend-Event"));
11+
if (request.headers.get("X-ZSend-Event") === "test") {
12+
return new Response("OK!");
1313
}
1414
//////////////////////////////////////////////
1515

1616
const secret = env.SECRET_KEY;
1717
if (!secret) {
18-
return new Response('Server configuration error: SECRET_KEY is not set', { status: 500 });
18+
return new Response("Server configuration error: SECRET_KEY is not set", { status: 500 });
1919
}
2020

21-
const signature = request.headers.get('x-zsend-signature') as string | null;
22-
const timestamp = request.headers.get('x-zsend-timestamp') as string | null;
21+
const signature = request.headers.get("x-zsend-signature") as string | null;
22+
const timestamp = request.headers.get("x-zsend-timestamp") as string | null;
2323

2424
if (!signature || !timestamp) {
25-
return new Response('Missing signature or timestamp', { status: 400 });
25+
return new Response("Missing signature or timestamp", { status: 400 });
2626
}
2727

2828
let body = {};
29-
let rawBody = '';
29+
let rawBody = "";
3030

3131
try {
3232
const clone = request.clone();
3333
body = await clone.json();
3434
rawBody = await request.text();
3535
} catch (e) {
36-
console.error('Failed JSON', { error: e, rawBody });
37-
return new Response('Invalid JSON', { status: 400 });
36+
console.error("Failed JSON", { error: e, rawBody });
37+
return new Response("Invalid JSON", { status: 400 });
3838
}
3939

4040
const message = `${timestamp}.${rawBody}`;
41-
const expectedSignature = crypto.createHmac('sha256', secret).update(message).digest('hex');
41+
const expectedSignature = crypto.createHmac("sha256", secret).update(message).digest("hex");
4242

4343
if (signature !== `sha256=${expectedSignature}`) {
44-
return new Response('Invalid signature', { status: 401 });
44+
return new Response("Invalid signature", { status: 401 });
4545
}
4646

4747
// 非同步執行
@@ -51,5 +51,5 @@ export async function router(request: Request, env: Env, ctx: ExecutionContext)
5151
})(),
5252
);
5353
// 回傳成功
54-
return new Response('OK!');
54+
return new Response("OK!");
5555
}

test/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const TEST_SECRET_KEY = 'test-secret-key';
1+
export const TEST_SECRET_KEY = "test-secret-key";

test/env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
declare module 'cloudflare:test' {
1+
declare module "cloudflare:test" {
22
interface ProvidedEnv extends Env {}
33
}

test/index.spec.ts

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,106 @@
1-
import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloudflare:test';
2-
import { describe, it, expect } from 'vitest';
3-
import crypto from 'crypto';
4-
import worker from '../src/_index';
5-
import { TEST_SECRET_KEY } from './constants';
1+
import { env, createExecutionContext, waitOnExecutionContext, SELF } from "cloudflare:test";
2+
import { describe, it, expect } from "vitest";
3+
import crypto from "crypto";
4+
import worker from "../src/_index";
5+
import { TEST_SECRET_KEY } from "./constants";
66

77
const IncomingRequest = Request<unknown, IncomingRequestCfProperties>;
88

9-
const timestamp = '1710000000';
10-
const payload = { id: 'evt_123', status: 'paid' };
9+
const timestamp = "1710000000";
10+
const payload = { id: "evt_123", status: "paid" };
1111
const rawBody = JSON.stringify(payload);
1212

1313
function signatureFor(secret: string, body = rawBody) {
14-
const digest = crypto.createHmac('sha256', secret).update(`${timestamp}.${body}`).digest('hex');
14+
const digest = crypto.createHmac("sha256", secret).update(`${timestamp}.${body}`).digest("hex");
1515
return `sha256=${digest}`;
1616
}
1717

1818
function signedRequest(body = rawBody, signature = signatureFor(TEST_SECRET_KEY, body)) {
19-
return new IncomingRequest('http://example.com/webhook', {
20-
method: 'POST',
19+
return new IncomingRequest("http://example.com/webhook", {
20+
method: "POST",
2121
headers: {
22-
'content-type': 'application/json',
23-
'x-zsend-event': 'payment.succeeded',
24-
'x-zsend-signature': signature,
25-
'x-zsend-timestamp': timestamp,
22+
"content-type": "application/json",
23+
"x-zsend-event": "payment.succeeded",
24+
"x-zsend-signature": signature,
25+
"x-zsend-timestamp": timestamp,
2626
},
2727
body,
2828
});
2929
}
3030

31-
describe('ZSend webhook worker', () => {
32-
it('rejects non-POST requests', async () => {
33-
const request = new IncomingRequest('http://example.com/webhook');
31+
describe("ZSend webhook worker", () => {
32+
it("rejects non-POST requests", async () => {
33+
const request = new IncomingRequest("http://example.com/webhook");
3434
const ctx = createExecutionContext();
3535
const response = await worker.fetch(request, env, ctx);
3636
await waitOnExecutionContext(ctx);
3737

3838
expect(response.status).toBe(405);
39-
expect(await response.text()).toBe('Method Not Allowed');
39+
expect(await response.text()).toBe("Method Not Allowed");
4040
});
4141

42-
it('rejects requests when SECRET_KEY is not configured', async () => {
42+
it("rejects requests when SECRET_KEY is not configured", async () => {
4343
const request = signedRequest();
4444
const ctx = createExecutionContext();
45-
const response = await worker.fetch(request, { ...env, SECRET_KEY: '' }, ctx);
45+
const response = await worker.fetch(request, { ...env, SECRET_KEY: "" }, ctx);
4646
await waitOnExecutionContext(ctx);
4747

4848
expect(response.status).toBe(500);
49-
expect(await response.text()).toBe('Server configuration error: SECRET_KEY is not set');
49+
expect(await response.text()).toBe("Server configuration error: SECRET_KEY is not set");
5050
});
5151

52-
it('rejects requests missing signature headers', async () => {
53-
const request = new IncomingRequest('http://example.com/webhook', {
54-
method: 'POST',
55-
headers: { 'content-type': 'application/json' },
52+
it("rejects requests missing signature headers", async () => {
53+
const request = new IncomingRequest("http://example.com/webhook", {
54+
method: "POST",
55+
headers: { "content-type": "application/json" },
5656
body: rawBody,
5757
});
5858
const ctx = createExecutionContext();
5959
const response = await worker.fetch(request, env, ctx);
6060
await waitOnExecutionContext(ctx);
6161

6262
expect(response.status).toBe(400);
63-
expect(await response.text()).toBe('Missing signature or timestamp');
63+
expect(await response.text()).toBe("Missing signature or timestamp");
6464
});
6565

66-
it('rejects invalid JSON bodies', async () => {
67-
const request = signedRequest('{bad json}', signatureFor(TEST_SECRET_KEY, '{bad json}'));
66+
it("rejects invalid JSON bodies", async () => {
67+
const request = signedRequest("{bad json}", signatureFor(TEST_SECRET_KEY, "{bad json}"));
6868
const ctx = createExecutionContext();
6969
const response = await worker.fetch(request, env, ctx);
7070
await waitOnExecutionContext(ctx);
7171

7272
expect(response.status).toBe(400);
73-
expect(await response.text()).toBe('Invalid JSON');
73+
expect(await response.text()).toBe("Invalid JSON");
7474
});
7575

76-
it('rejects requests with invalid signatures', async () => {
77-
const request = signedRequest(rawBody, 'invalid-signature');
76+
it("rejects requests with invalid signatures", async () => {
77+
const request = signedRequest(rawBody, "invalid-signature");
7878
const ctx = createExecutionContext();
7979
const response = await worker.fetch(request, env, ctx);
8080
await waitOnExecutionContext(ctx);
8181

8282
expect(response.status).toBe(401);
83-
expect(await response.text()).toBe('Invalid signature');
83+
expect(await response.text()).toBe("Invalid signature");
8484
});
8585

86-
it('accepts valid signed webhook requests in unit style', async () => {
86+
it("accepts valid signed webhook requests in unit style", async () => {
8787
const request = signedRequest();
8888
const ctx = createExecutionContext();
8989
const response = await worker.fetch(request, env, ctx);
9090
await waitOnExecutionContext(ctx);
9191

9292
expect(response.status).toBe(200);
93-
expect(await response.text()).toBe('OK!');
93+
expect(await response.text()).toBe("OK!");
9494
});
9595

96-
it('accepts valid signed webhook requests in integration style', async () => {
97-
const response = await SELF.fetch('https://example.com/webhook', {
98-
method: 'POST',
96+
it("accepts valid signed webhook requests in integration style", async () => {
97+
const response = await SELF.fetch("https://example.com/webhook", {
98+
method: "POST",
9999
headers: signedRequest().headers,
100100
body: rawBody,
101101
});
102102

103103
expect(response.status).toBe(200);
104-
expect(await response.text()).toBe('OK!');
104+
expect(await response.text()).toBe("OK!");
105105
});
106106
});

vitest.config.mts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { cloudflareTest } from '@cloudflare/vitest-pool-workers';
2-
import { defineConfig } from 'vitest/config';
3-
import { TEST_SECRET_KEY } from './test/constants';
1+
import { cloudflareTest } from "@cloudflare/vitest-pool-workers";
2+
import { defineConfig } from "vitest/config";
3+
import { TEST_SECRET_KEY } from "./test/constants";
44

55
process.env.SECRET_KEY ??= TEST_SECRET_KEY;
66

77
export default defineConfig({
88
plugins: [
99
cloudflareTest({
10-
wrangler: { configPath: './wrangler.jsonc' },
10+
wrangler: { configPath: "./wrangler.jsonc" },
1111
miniflare: {
1212
bindings: {
1313
SECRET_KEY: TEST_SECRET_KEY,

0 commit comments

Comments
 (0)