feat: create function to handle fetching data from multiple sources
This helps since sometimes the data sources have issues occasionally
This commit is contained in:
92
src/libs/fetchFromMultipleSources.spec.ts
Normal file
92
src/libs/fetchFromMultipleSources.spec.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
|
||||
import { fetchFromMultipleSources } from "./fetchFromMultipleSources";
|
||||
|
||||
describe("fetchFromMultipleSources", () => {
|
||||
it("no promises, throws exception", () => {
|
||||
expect(() => fetchFromMultipleSources([])).toThrow(
|
||||
"fetchPromises cannot be empty",
|
||||
);
|
||||
});
|
||||
|
||||
it("has promises, returns first one with value", async () => {
|
||||
const actual = await fetchFromMultipleSources<number>([
|
||||
() => Promise.resolve(undefined),
|
||||
() => Promise.resolve(null),
|
||||
() => Promise.reject(),
|
||||
() => Promise.resolve(2),
|
||||
() => Promise.resolve(3),
|
||||
]);
|
||||
const expected = 2;
|
||||
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it("has promises, no valid responses, returns null", async () => {
|
||||
const actual = await fetchFromMultipleSources<number>([
|
||||
() => Promise.resolve(null),
|
||||
() => Promise.reject(),
|
||||
() => Promise.resolve(undefined),
|
||||
]);
|
||||
const expected = null;
|
||||
|
||||
expect(actual).toBe(expected);
|
||||
});
|
||||
|
||||
it("has promises, has cached value, returns cached value", async () => {
|
||||
const actual = await fetchFromMultipleSources<number>(
|
||||
[
|
||||
() => Promise.resolve(null),
|
||||
() => Promise.reject(),
|
||||
() => Promise.resolve(undefined),
|
||||
],
|
||||
{
|
||||
fetchFromCache: () => Promise.resolve(-1),
|
||||
saveInCache: async () => {},
|
||||
},
|
||||
);
|
||||
const expected = -1;
|
||||
|
||||
expect(actual).toBe(expected);
|
||||
});
|
||||
|
||||
it("has promises, no cached value, no valid response, should not save in cache", async () => {
|
||||
let actual;
|
||||
await fetchFromMultipleSources<number>(
|
||||
[
|
||||
() => Promise.resolve(null),
|
||||
() => Promise.reject(),
|
||||
() => Promise.resolve(undefined),
|
||||
],
|
||||
{
|
||||
fetchFromCache: () => Promise.resolve(null),
|
||||
saveInCache: async (value) => {
|
||||
actual = value;
|
||||
},
|
||||
},
|
||||
);
|
||||
const expected = undefined;
|
||||
|
||||
expect(actual).toBe(expected);
|
||||
});
|
||||
|
||||
it("has promises, no cached value, has valid response, should save in cache", async () => {
|
||||
let actual: number | undefined;
|
||||
await fetchFromMultipleSources<number>(
|
||||
[
|
||||
() => Promise.resolve(null),
|
||||
() => Promise.reject(),
|
||||
() => Promise.resolve(3),
|
||||
],
|
||||
{
|
||||
fetchFromCache: () => Promise.resolve(null),
|
||||
saveInCache: async (value) => {
|
||||
actual = value;
|
||||
},
|
||||
},
|
||||
);
|
||||
const expected = 3;
|
||||
|
||||
expect(actual).toBe(expected);
|
||||
});
|
||||
});
|
||||
36
src/libs/fetchFromMultipleSources.ts
Normal file
36
src/libs/fetchFromMultipleSources.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
type OptionalArgs<T> =
|
||||
| {
|
||||
saveInCache: (result: NonNullable<T>) => Promise<void>;
|
||||
fetchFromCache: () => Promise<T | null | undefined>;
|
||||
}
|
||||
| {
|
||||
saveInCache?: undefined;
|
||||
fetchFromCache?: undefined;
|
||||
};
|
||||
|
||||
export async function fetchFromMultipleSources<T>(
|
||||
fetchPromises: (() => Promise<T | null | undefined>)[],
|
||||
args?: OptionalArgs<T>,
|
||||
) {
|
||||
let result = await args?.fetchFromCache?.();
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (fetchPromises.length === 0) {
|
||||
throw new Error("fetchPromises cannot be empty");
|
||||
}
|
||||
|
||||
for (const promise of fetchPromises) {
|
||||
try {
|
||||
result = await promise();
|
||||
if (result) break;
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
if (args?.saveInCache && result) {
|
||||
await args.saveInCache(result);
|
||||
}
|
||||
|
||||
return result ?? null;
|
||||
}
|
||||
Reference in New Issue
Block a user