app -> route

This commit is contained in:
Alex Gleason 2025-02-21 15:08:37 -06:00
parent d0c7cc7a45
commit e5657d67c0
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 26 additions and 26 deletions

View file

@ -10,19 +10,19 @@ import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools';
import { createTestDB } from '@/test.ts';
import cashuApp from '@/controllers/api/cashu.ts';
import cashuRoute from './cashu.ts';
import { walletSchema } from '@/schema.ts';
Deno.test('PUT /wallet must be successful', {
sanitizeOps: false,
sanitizeResources: false,
}, async () => {
await using test = await createTestApp();
await using test = await createTestRoute();
const { app, signer, sk, relay } = test;
const { route, signer, sk, relay } = test;
const nostrPrivateKey = bytesToString('hex', sk);
const response = await app.request('/wallet', {
const response = await route.request('/wallet', {
method: 'PUT',
headers: {
'content-type': 'application/json',
@ -82,10 +82,10 @@ Deno.test('PUT /wallet must be successful', {
});
Deno.test('PUT /wallet must NOT be successful: wrong request body/schema', async () => {
await using test = await createTestApp();
const { app } = test;
await using test = await createTestRoute();
const { route } = test;
const response = await app.request('/wallet', {
const response = await route.request('/wallet', {
method: 'PUT',
headers: {
'content-type': 'application/json',
@ -105,12 +105,12 @@ Deno.test('PUT /wallet must NOT be successful: wallet already exists', {
sanitizeOps: false,
sanitizeResources: false,
}, async () => {
await using test = await createTestApp();
const { app, sk, relay } = test;
await using test = await createTestRoute();
const { route, sk, relay } = test;
await relay.event(genEvent({ kind: 17375 }, sk));
const response = await app.request('/wallet', {
const response = await route.request('/wallet', {
method: 'PUT',
headers: {
'authorization': `Bearer ${nip19.nsecEncode(sk)}`,
@ -131,8 +131,8 @@ Deno.test('GET /wallet must be successful', {
sanitizeOps: false,
sanitizeResources: false,
}, async () => {
await using test = await createTestApp();
const { app, sk, relay, signer } = test;
await using test = await createTestRoute();
const { route, sk, relay, signer } = test;
const pubkey = await signer.getPublicKey();
const privkey = bytesToString('hex', sk);
@ -214,7 +214,7 @@ Deno.test('GET /wallet must be successful', {
],
}, senderSk));
const response = await app.request('/wallet', {
const response = await route.request('/wallet', {
method: 'GET',
});
@ -230,10 +230,10 @@ Deno.test('GET /wallet must be successful', {
});
Deno.test('GET /mints must be successful', async () => {
await using test = await createTestApp();
const { app } = test;
await using test = await createTestRoute();
const { route } = test;
const response = await app.request('/mints', {
const response = await route.request('/mints', {
method: 'GET',
});
@ -243,7 +243,7 @@ Deno.test('GET /mints must be successful', async () => {
assertEquals(body, { mints: [] });
});
async function createTestApp() {
async function createTestRoute() {
const conf = new DittoConf(new Map());
const db = await createTestDB();
@ -252,17 +252,17 @@ async function createTestApp() {
const sk = generateSecretKey();
const signer = new NSecSigner(sk);
const app = new DittoApp({ db, relay, conf });
const route = new DittoApp({ db, relay, conf });
app.use(testUserMiddleware({ signer, relay }));
app.route('/', cashuApp);
route.use(testUserMiddleware({ signer, relay }));
route.route('/', cashuRoute);
const mock = stub(globalThis, 'fetch', () => {
return Promise.resolve(new Response());
});
return {
app,
route,
db,
conf,
sk,

View file

@ -14,7 +14,7 @@ import { errorJson } from '@/utils/log.ts';
type Wallet = z.infer<typeof walletSchema>;
const app = new DittoRoute();
const route = new DittoRoute();
// app.delete('/wallet') -> 204
@ -42,7 +42,7 @@ const createCashuWalletAndNutzapInfoSchema = z.object({
* https://github.com/nostr-protocol/nips/blob/master/60.md
* https://github.com/nostr-protocol/nips/blob/master/61.md#nutzap-informational-event
*/
app.put('/wallet', userMiddleware('nip44'), async (c) => {
route.put('/wallet', userMiddleware('nip44'), async (c) => {
const { conf, user, relay, signal } = c.var;
const pubkey = await user.signer.getPublicKey();
@ -104,7 +104,7 @@ app.put('/wallet', userMiddleware('nip44'), async (c) => {
});
/** Gets a wallet, if it exists. */
app.get('/wallet', userMiddleware('nip44'), swapNutzapsMiddleware, async (c) => {
route.get('/wallet', userMiddleware('nip44'), swapNutzapsMiddleware, async (c) => {
const { conf, relay, user, signal } = c.var;
const pubkey = await user.signer.getPublicKey();
@ -157,7 +157,7 @@ app.get('/wallet', userMiddleware('nip44'), swapNutzapsMiddleware, async (c) =>
});
/** Get mints set by the CASHU_MINTS environment variable. */
app.get('/mints', (c) => {
route.get('/mints', (c) => {
const { conf } = c.var;
// TODO: Return full Mint information: https://github.com/cashubtc/nuts/blob/main/06.md
@ -166,4 +166,4 @@ app.get('/mints', (c) => {
return c.json({ mints }, 200);
});
export default app;
export default route;