From 1ca9a7225c5a97ea363e7cf2a011f7bd7f715f3d Mon Sep 17 00:00:00 2001 From: ntr Date: Tue, 7 Feb 2023 18:23:54 +1100 Subject: [PATCH] env files, fixes for discogs sync --- .gitignore | 4 +++- api/src/api.test.ts | 8 -------- api/src/api.ts | 7 ++++++- api/src/discogs.ts | 16 ++++++++++++++++ api/src/utils.ts | 2 ++ docker-compose.yaml | 8 ++++---- 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index ca06f06..b936a6f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ node_modules/ data/ -db/ \ No newline at end of file +db/ + +.env \ No newline at end of file diff --git a/api/src/api.test.ts b/api/src/api.test.ts index fe88d4d..97fd835 100644 --- a/api/src/api.test.ts +++ b/api/src/api.test.ts @@ -48,12 +48,4 @@ describe('api', function apiTests() { assert(res.headers['set-cookie']); }) }); - - describe('offers', function offerTests() { - it('create offers', async function createOffersTest() { - const { data: bundles } = await api.get('/bundles'); - const listingIds = bundles[0].items.map(i => i.id); - const { data } = await api.post('/offers', { listingIds, shipping: 50.0, baseShipping: 15.0 }); - }); - }); }); diff --git a/api/src/api.ts b/api/src/api.ts index f4bc9d9..a413364 100644 --- a/api/src/api.ts +++ b/api/src/api.ts @@ -29,6 +29,10 @@ export default function createApi(log: Log, pg: Pg, discogs: Discogs, offers: Of const app = koaWebsocket(new koa()); + BigInt.prototype.toJSON = function bigIntStringify() { + return this.toString(); + }; + app.use(async (ctx, next) => { try { await next(); @@ -46,6 +50,7 @@ export default function createApi(log: Log, pg: Pg, discogs: Discogs, offers: Of function listen() { const server = app.listen(PORT); log.info({ PORT }, 'listening'); + return server; } app.use(cors({ credentials: true })); @@ -68,7 +73,7 @@ export default function createApi(log: Log, pg: Pg, discogs: Discogs, offers: Of const { username } = ctx.request.body; const user = await pg.userFindId(ctx.state.user.id); const wants = await discogs.wantListSync(username, user); - events.emit(Events.WantsAdded, [wants]); + events.emit(Events.WantsAdded, wants); ctx.response.body = wants; }); diff --git a/api/src/discogs.ts b/api/src/discogs.ts index 5aa662b..7edf3ae 100644 --- a/api/src/discogs.ts +++ b/api/src/discogs.ts @@ -27,6 +27,21 @@ export default function createDiscogs(log: Log, pg: Pg) { }, }); + function onSuccess(response) { + return response; + } + + function onError(error) { + // if (error.response) return [error.response.data.error, null]; + // if (error.request) return ['Network Error', null]; + // return ['Unknown Error', null]; + if (error.response.status === 429) { + throw new Error('rate limited, try again in 1 minute'); + } + } + + api.interceptors.response.use(onSuccess, onError); + async function handleApiError(e) { logAxiosError(e); if (e.response.status === 429) { @@ -45,6 +60,7 @@ export default function createDiscogs(log: Log, pg: Pg) { // convert want object to actual release and ignore duplicate errors const results = await Promise.allSettled(releases.map(release => pg.wantCreate(release, user))); + log.info(results); // @ts-ignore const additions = results.filter(r => r.status === 'fulfilled').map(r => r.value); diff --git a/api/src/utils.ts b/api/src/utils.ts index 0c06b96..7c3f014 100644 --- a/api/src/utils.ts +++ b/api/src/utils.ts @@ -17,6 +17,8 @@ export function logAxiosError(e) { export function wantDetails(want: Want) { const data = want.data as any; + if (!data) console.warn(want); + return { title: data.title, release: data.id, diff --git a/docker-compose.yaml b/docker-compose.yaml index 27f8e45..acfb5a5 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -4,9 +4,9 @@ services: pg: image: postgres:latest environment: - POSTGRES_USER: 'mcrates' - POSTGRES_DB: 'mcrates' - POSTGRES_PASSWORD: 'digdeep' + POSTGRES_USER: '${POSTGRES_USER}' + POSTGRES_DB: '${POSTGRES_DB}' + POSTGRES_PASSWORD: '${POSTGRES_PASSWORD}' ports: - '45432:5432' volumes: @@ -19,7 +19,7 @@ services: - 'pg' entrypoint: npm start environment: - DATABASE_URL: 'postgresql://mcrates:digdeep@pg:5432/mcrates?schema=public' + DATABASE_URL: 'postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@pg:5432/${POSTGRES_DB}?schema=public' volumes: - "./api:/opt/mcrates/api"