From c562f2be5200dc7a339845a04ff8d5d9684d5dd1 Mon Sep 17 00:00:00 2001 From: Angelo Reale <12191809+angeloreale@users.noreply.github.com> Date: Mon, 5 Aug 2024 16:45:05 +0100 Subject: [PATCH] =?UTF-8?q?ar(fix)=20[DPCP-44]=20[DPCP-24]:=20Nyx=20Timeou?= =?UTF-8?q?t=20on=20Hydration,=20Audio=20Player=20disable=20au=E2=80=A6=20?= =?UTF-8?q?(#26)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ar(fix) DPCP-44-24: Nyx Timeout on Hydration, Audio Player disable autoPlay. * ar(fix) DPCP-44-24: Nyx Timeout on Hydration, Audio Player disable autoPlay. * ar(fix) DPCP-44-24: Nyx Timeout on Hydration, Audio Player disable autoPlay. * ar(fix) DPCP-44-24: Nyx Timeout on Hydration, Audio Player disable autoPlay. * ar(fix) DPCP-44-24: Nyx Timeout on Hydration, Audio Player disable autoPlay. --- lib/model/interfaces/helpers.ts | 17 +++++++++++++++++ .../private/hypnos-private-user-connector.ts | 4 ++-- .../hypnos/public/hypnos-public-connector.ts | 3 ++- .../services/rickmorty/rm-connector.ts | 3 ++- package-lock.json | 8 ++++---- package.json | 2 +- 6 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/model/interfaces/helpers.ts b/lib/model/interfaces/helpers.ts index 3bd1c29b..f36045b3 100644 --- a/lib/model/interfaces/helpers.ts +++ b/lib/model/interfaces/helpers.ts @@ -2,6 +2,23 @@ // to-do: let's make an @helpers alias! // import { NexusDB } from "@controller" +export async function fetchWithTimeout(resource: string, options: any) { + const { timeout = 3000 } = options; + + const controller = new AbortController(); + const id = setTimeout(() => controller.abort(), timeout); + + const promise = await fetch(resource, { + ...options, + signal: controller.signal, + }); + + const response = promise; + clearTimeout(id); + + return response; +} + export const patience = async (sleep = 1000): Promise => { // if (NexusDB && !NexusDB.oplog?.length) { // if (NexusDB) { diff --git a/lib/model/interfaces/services/hypnos/private/hypnos-private-user-connector.ts b/lib/model/interfaces/services/hypnos/private/hypnos-private-user-connector.ts index c1f122a3..3055fd10 100644 --- a/lib/model/interfaces/services/hypnos/private/hypnos-private-user-connector.ts +++ b/lib/model/interfaces/services/hypnos/private/hypnos-private-user-connector.ts @@ -2,8 +2,8 @@ // rm-connector.ts // to-do: use prisma for graph type import type { ICard } from '@dreampipcom/oneiros'; - import { cookies } from 'next/headers'; +import { fetchWithTimeout } from '../../../helpers'; // const CHARS = ` // query { // characters() { @@ -32,7 +32,7 @@ async function fetchREPL({ paramsStr, method, listings }: any) { const cookieStore = cookies(); const cookieString = cookieStore.toString(); const payload = JSON.stringify({ listings }); - const req = await fetch(`${process.env.API_HOST}/api/v1/user${paramsStr}`, { + const req = await fetchWithTimeout(`${process.env.API_HOST}/api/v1/user${paramsStr}`, { method, headers: { 'Content-Type': 'application/json', diff --git a/lib/model/interfaces/services/hypnos/public/hypnos-public-connector.ts b/lib/model/interfaces/services/hypnos/public/hypnos-public-connector.ts index 123b676f..24c60eea 100644 --- a/lib/model/interfaces/services/hypnos/public/hypnos-public-connector.ts +++ b/lib/model/interfaces/services/hypnos/public/hypnos-public-connector.ts @@ -1,6 +1,7 @@ // rm-connector.ts // to-do: use prisma for graph type import type { ICard } from '@dreampipcom/oneiros'; +import { fetchWithTimeout } from '../../../helpers'; // const CHARS = ` // query { // characters() { @@ -26,7 +27,7 @@ import type { ICard } from '@dreampipcom/oneiros'; async function fetchREPL({ paramsStr }: any) { // to-do: might be worth hardcoding the api in case too many middleware requests are billed try { - const response = await fetch(`${process.env.API_HOST}/api/v1/public${paramsStr}`, { + const response = await fetchWithTimeout(`${process.env.API_HOST}/api/v1/public${paramsStr}`, { method: 'GET', headers: { 'Content-Type': 'application/json', diff --git a/lib/model/interfaces/services/rickmorty/rm-connector.ts b/lib/model/interfaces/services/rickmorty/rm-connector.ts index 13560ba5..da244c9b 100644 --- a/lib/model/interfaces/services/rickmorty/rm-connector.ts +++ b/lib/model/interfaces/services/rickmorty/rm-connector.ts @@ -1,6 +1,7 @@ // rm-connector.ts // to-do: use prisma for graph type import type { INCharacter } from '@types'; +import { fetchWithTimeout } from '../../helpers'; const CHARS = ` query { characters() { @@ -25,7 +26,7 @@ query { async function fetchGraphQL(query: string) { try { - return fetch(`https://rickandmortyapi.com/graphql`, { + return fetchWithTimeout(`https://rickandmortyapi.com/graphql`, { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/package-lock.json b/package-lock.json index 928e6317..0e0ea542 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.0", "license": "HPL3-ECO-AND-ANC", "dependencies": { - "@dreampipcom/oneiros": "0.0.12", + "@dreampipcom/oneiros": "0.0.14", "@sentry/nextjs": "7.118.0", "next": "14.2.4", "react": "18", @@ -377,9 +377,9 @@ } }, "node_modules/@dreampipcom/oneiros": { - "version": "0.0.12", - "resolved": "https://npm.pkg.github.com/download/@dreampipcom/oneiros/0.0.12/b0acf5ed8fce9928b39498dbb8a71f632b4bbd92", - "integrity": "sha512-U00GcnU63hB/G9U+GPpqcRW2H28w72w4pc8ma5gRnfb0d28fygx0NSOl5l+1rk+25XHVXQBlRACCfPFWgctNQw==", + "version": "0.0.14", + "resolved": "https://npm.pkg.github.com/download/@dreampipcom/oneiros/0.0.14/2ea460bff6882ae001aab93a642db6d1544bc529", + "integrity": "sha512-j5z+b/llOD6aXd+zPWxi0SObOKzOVpoWIxTdO/a+b+OKRdiTEdWIDuH5wcUe8U0ur4XVlswnOtuWimVwq8/ijQ==", "license": "HPL3-ECO-AND-ANC", "optionalDependencies": { "@rollup/rollup-linux-x64-gnu": "4.9.5" diff --git a/package.json b/package.json index 80aa1d56..1fe8dce9 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "prepare": "husky" }, "dependencies": { - "@dreampipcom/oneiros": "0.0.12", + "@dreampipcom/oneiros": "0.0.14", "@sentry/nextjs": "7.118.0", "next": "14.2.4", "react": "18",