-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathlocal.ts
36 lines (32 loc) · 950 Bytes
/
local.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE-examples file in the root directory of this source tree.
*
* @flow strict
*/
import swapiData from '../../cache/data.json';
/**
* Given a URL of an object in the SWAPI, return the data
* from our local cache.
*/
// casting json file to a Record with Index.
const typedSwapiData = swapiData as typeof swapiData & { [key: string]: any }
export async function getFromLocalUrl(
url: unknown,
): Promise<Record<string, any>> {
if (!(typeof url === 'string')) {
throw new Error('Url provided is not a string');
}
const text = typedSwapiData[url];
if (!text) {
throw new Error(`No entry in local cache for ${url}`);
}
if (process.env.NODE_ENV !== 'test') {
// eslint-disable-next-line no-console
console.log(`Hit the local cache for ${url}.`);
}
return text;
}