forked from timothymiller/t4-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
163 additions
and
10 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
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,20 @@ | ||
export default { | ||
preset: 'ts-jest/presets/default-esm', | ||
transform: { | ||
'^.+\\.ts$': [ | ||
'ts-jest', | ||
{ | ||
tsconfig: 'test/tsconfig.json', | ||
useESM: true, | ||
}, | ||
], | ||
}, | ||
moduleNameMapper: { | ||
'^@/(.*)$': '<rootDir>/src/$1', | ||
'^(\\.{1,2}/.*)\\.js$': '$1', | ||
}, | ||
testEnvironment: 'miniflare', | ||
testEnvironmentOptions: { | ||
modules: true, | ||
}, | ||
} |
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
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,8 @@ | ||
import { Bindings } from '../src' | ||
|
||
declare global { | ||
function getMiniflareBindings(): Bindings | ||
function getMiniflareDurableObjectStorage(id: DurableObjectId): Promise<DurableObjectStorage> | ||
} | ||
|
||
export {} |
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,66 @@ | ||
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client' | ||
import jwt from '@tsndr/cloudflare-worker-jwt' | ||
import * as dotenv from 'dotenv' | ||
import { readMigrationFiles } from 'drizzle-orm/migrator' | ||
import superjson from 'superjson' | ||
import app, { Bindings } from '../src' | ||
import { createDb } from '../src/db/client' | ||
import { AppRouter } from '../src/router' | ||
dotenv.config({ path: './.dev.vars' }) | ||
|
||
const bindings = getMiniflareBindings() | ||
|
||
export const env: Bindings = { | ||
...bindings, | ||
// allow tests to work without env vars | ||
APP_URL: bindings.APP_URL || process.env.APP_URL || 'http://localhost:3000', | ||
JWT_VERIFICATION_KEY: | ||
bindings.JWT_VERIFICATION_KEY || process.env.JWT_VERIFICATION_KEY || '12345', | ||
} | ||
export const db = createDb(env.__D1_BETA__DB) | ||
|
||
export async function executeSql(sql: string) { | ||
return await env.__D1_BETA__DB.exec(sql) | ||
} | ||
|
||
export async function migrateDb() { | ||
const files = readMigrationFiles({ migrationsFolder: './migrations' }) | ||
for (let i = 0; i < files.length; i++) { | ||
const migrationMeta = files[i] | ||
for (let j = 0; j < migrationMeta.sql.length; j++) { | ||
await executeSql(migrationMeta.sql[j].replaceAll(/[\n\t]/g, '')) | ||
} | ||
} | ||
} | ||
|
||
export async function createSessionToken({ userId = 'test-user' }: { userId?: string }) { | ||
const payload = { | ||
exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 7, | ||
sub: userId, | ||
} | ||
const algorithm = 'HS256' | ||
return await jwt.sign(payload, env.JWT_VERIFICATION_KEY, { algorithm }) | ||
} | ||
|
||
export function createTRPCClient({ | ||
userId, | ||
url = 'http://localhost:3000/trpc', | ||
}: { | ||
userId?: string | ||
url?: string | ||
}) { | ||
return createTRPCProxyClient<AppRouter>({ | ||
transformer: superjson, | ||
links: [ | ||
httpBatchLink({ | ||
url, | ||
async headers() { | ||
return { | ||
authorization: userId ? 'Bearer ' + (await createSessionToken({ userId })) : undefined, | ||
} | ||
}, | ||
fetch: async (resource, options) => app.request(resource, options, env), | ||
}), | ||
], | ||
}) | ||
} |
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 @@ | ||
import { env } from './helpers' | ||
|
||
describe('T4 App API Server', () => { | ||
test('has cloudflare bindings', async () => { | ||
expect(env.__D1_BETA__DB).toBeTruthy() | ||
}) | ||
test('has env vars', async () => { | ||
expect(env.JWT_VERIFICATION_KEY).toBeTruthy() | ||
}) | ||
}) |
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,13 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"types": [ | ||
"@cloudflare/workers-types", | ||
"node", | ||
"jest", | ||
"./globals.d.ts", | ||
"../worker-configuration.d.ts" | ||
] | ||
}, | ||
"include": ["../src/**/*", "**/*"] | ||
} |
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,19 @@ | ||
import { UserTable } from '../src/db/schema' | ||
import { createSessionToken, createTRPCClient, db, migrateDb } from './helpers' | ||
|
||
// Note: if your routers make any external requests, you can mock them with getMiniflareFetchMock() | ||
// https://github.com/cloudflare/miniflare/blob/master/docs/src/content/testing/jest.md#mocking-outbound-fetch-requests | ||
|
||
describe('User router', () => { | ||
beforeAll(async () => { | ||
await migrateDb() | ||
}) | ||
|
||
test('current', async () => { | ||
const userId = 'test-user' | ||
await db.insert(UserTable).values({ id: userId, email: '[email protected]' }) | ||
const client = createTRPCClient({ userId }) | ||
const res = await client.user.current.query() | ||
expect(res?.id).toBe(userId) | ||
}) | ||
}) |
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,4 @@ | ||
// Generated by Wrangler on Tue Oct 03 2023 20:10:16 GMT-0400 (Eastern Daylight Time) | ||
interface Env { | ||
__D1_BETA__DB: D1Database; | ||
} |
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