From 2ffb5f9bc3fb34b60417d1e268ec8a7b20306ea7 Mon Sep 17 00:00:00 2001 From: ntr Date: Wed, 8 Feb 2023 22:51:09 +1100 Subject: [PATCH] use [err, res] pattern for server axios --- TODO.md | 3 +++ api/src/discogs.ts | 58 ++++++++++++++++++++++++++++++---------------- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/TODO.md b/TODO.md index 526b054..3c6fcdf 100644 --- a/TODO.md +++ b/TODO.md @@ -8,6 +8,9 @@ discord bot email ppl who signed up say we have updates +order by dropdown for main list +sample offer with tooltips + go over and reorganise event logic should be connection.service ? diff --git a/api/src/discogs.ts b/api/src/discogs.ts index d014464..df2af97 100644 --- a/api/src/discogs.ts +++ b/api/src/discogs.ts @@ -10,9 +10,19 @@ import type { PrismaClient, Want, User, Listing } from '@prisma/client'; import { logAxiosError, wantDetails } from './utils'; import { Pg } from './pg'; -// margh -// vanderbabe -// jmcdermid1 +type ApiResponse = [string | null, any | null]; + +declare module 'axios' { + export interface AxiosInstance { + request(config: AxiosRequestConfig): Promise; + get(url: string, config?: AxiosRequestConfig): Promise; + delete(url: string, config?: AxiosRequestConfig): Promise; + head(url: string, config?: AxiosRequestConfig): Promise; + post(url: string, data?: any, config?: AxiosRequestConfig): Promise; + put(url: string, data?: any, config?: AxiosRequestConfig): Promise; + patch(url: string, data?: any, config?: AxiosRequestConfig): Promise; + } +} export default function createDiscogs(log: Log, pg: Pg) { const TOKEN = 'Discogs token=wRTXuYZGOPsIQgKRGKCAkUkidGtvpSfQgPWkYIpD'; @@ -28,24 +38,27 @@ export default function createDiscogs(log: Log, pg: Pg) { }); function onSuccess(response) { - return response; + return [null, response.data]; } function onError(error) { - // if (error.response) return [error.response.data.error, null]; - // if (error.request) return ['Network Error', null]; - // return ['Unknown Error', null]; + console.error(error); if (error.response.status === 429) { - throw new Error('rate limited, try again in 1 minute'); + return [new Error('rate limited, try again in 1 minute'), null]; } + if (error.response) return [error.response.data, null]; + if (error.request) return ['Network Error', null]; + return ['Unknown Error', null]; } - api.interceptors.response.use(onSuccess, onError); async function wantListSync(username: string, user: User): Promise { - const { data: { wants, pagination }} = await api.get(`${apiBase}/users/${username}/wants`); - const releases = await Promise.all(wants.map(want => release(want.id))); + const [err, res] = await api.get(`${apiBase}/users/${username}/wants`); + log.warn(err, res); + if (err) throw err; + const { wants, pagination } = res; + const releases = await Promise.all(wants.map(want => release(want.id))); log.info({ wants: wants.length, user: username, userId: user.id }, 'fetched discogs wantlist'); // TODO filter releases to be vinyl format only @@ -62,7 +75,7 @@ export default function createDiscogs(log: Log, pg: Pg) { } async function release(id: DiscogsRelease): Promise { - const { data } = await api.get(`${apiBase}/releases/${id}`); + const [error, data] = await api.get(`${apiBase}/releases/${id}`); return data; } @@ -71,18 +84,23 @@ export default function createDiscogs(log: Log, pg: Pg) { url.searchParams.append('query', query); url.searchParams.append('type', 'release'); url.searchParams.append('format', 'vinyl'); - const { data: { results } } = await api.get(url.href); + const [err, res] = await api.get(url.href); + if (err) throw err; + const { results } = res; return results; } async function inventory(user: string) { - const res = await api.get(`${apiBase}/users/${user}/inventory`); - return res.data.listings; + const [err, res] = await api.get(`${apiBase}/users/${user}/inventory`); + if (err) throw err; + return res.listings; } async function listings(want: Want) { - const res = await api.get(`https://www.discogs.com/sell/release/${want.release}?limit=250`); - const $ = cheerio.load(res.data); + const [err, res] = await api.get(`https://www.discogs.com/sell/release/${want.release}?limit=250`); + if (err) throw err; + + const $ = cheerio.load(res); const ids = $('.item_description_title[href]') .map(function getListingId() { const id = Number.parseInt(this.attribs.href.replace('/sell/item/', ''), 10); @@ -102,9 +120,9 @@ export default function createDiscogs(log: Log, pg: Pg) { if (listing) return listing; log.debug('fetching listing', id); - const res = await api.get(`${apiBase}/marketplace/listings/${id}`); - log.debug(res.data); - return pg.listingUpsert(res.data, want); + const [err, discogsListing] = await api.get(`${apiBase}/marketplace/listings/${id}`); + log.debug(discogsListing); + return pg.listingUpsert(discogsListing, want); } return {