-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathflashApi.js
More file actions
124 lines (108 loc) · 4.29 KB
/
Copy pathflashApi.js
File metadata and controls
124 lines (108 loc) · 4.29 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import store from '@/store';
import {
decodeEmbeddedFlashId,
decodeEmbeddedPartNumber,
getEmbeddedInfo,
searchEmbeddedFlashId,
searchEmbeddedPartNumber,
warmEmbeddedParser as warmEmbeddedFdnextParser
} from '@/services/fdnextApi';
import { FDNEXT_CAPABILITIES_SCHEMA_VERSIONS, summaryText } from '@/services/fdnextResultView';
import { DEFAULT_HTTP_REQUEST_TIMEOUT_MS, runWithRequestTimeout } from '@/services/requestControl';
const makeUrl = (endpoint, params = {}) => {
const base = store.getServerAddress().replace(/\/+$/, '');
const url = new URL(`${base}/${endpoint.replace(/^\/+/, '')}`);
for (const [key, value] of Object.entries(params)) {
if (value !== undefined && value !== null && value !== '') {
url.searchParams.set(key, value);
}
}
return url.toString();
};
const assertFdnextPayload = (payload, schemaVersion, endpoint) => {
const schemaVersions = Array.isArray(schemaVersion) ? schemaVersion : [schemaVersion];
if (!payload || !schemaVersions.includes(payload.schemaVersion)) {
throw new Error(`Unsupported fdnext response from ${endpoint}`);
}
return payload;
};
const parseResponsePayload = async response => {
const text = await response.text();
if (!text) return null;
try {
return JSON.parse(text);
} catch {
return null;
}
};
const request = async (endpoint, params = {}, schemaVersion = 'fdnext.result.v1', options = {}) => {
return runWithRequestTimeout(async signal => {
const response = await fetch(makeUrl(endpoint, params), { signal });
const payload = await parseResponsePayload(response);
if (payload) {
try {
return assertFdnextPayload(payload, schemaVersion, endpoint);
} catch (err) {
if (response.ok) throw err;
}
}
if (!response.ok) throw new Error(`${response.status} ${response.statusText}`);
throw new Error(`Unsupported fdnext response from ${endpoint}`);
}, {
signal: options.signal,
timeoutMs: options.timeoutMs ?? DEFAULT_HTTP_REQUEST_TIMEOUT_MS,
timeoutMessage: `Request to ${endpoint} timed out.`
});
};
const useEmbeddedParser = () => store.isEmbeddedParser();
const langParams = () => ({
lang: store.getLang()
});
const limitParams = limit => {
const value = Number(limit);
return Number.isFinite(value) && value > 0 ? { limit: value } : {};
};
const controllerGroupParams = () => ({
controllerGroup: store.getControllerGroupParam()
});
export const getServerInfo = async (options = {}) => useEmbeddedParser()
? getEmbeddedInfo()
: request('capabilities', langParams(), FDNEXT_CAPABILITIES_SCHEMA_VERSIONS, options);
export const warmEmbeddedParser = () => {
if (!useEmbeddedParser()) return Promise.resolve();
return warmEmbeddedFdnextParser();
};
export const decodePartNumber = async (pn, options = {}) => {
return useEmbeddedParser() ? decodeEmbeddedPartNumber(pn) : request('parts/decode', {
...langParams(),
...controllerGroupParams(),
query: pn
}, 'fdnext.result.v1', options);
};
export const searchPartNumber = async (pn, limit = 0, options = {}) => {
return useEmbeddedParser() ? searchEmbeddedPartNumber(pn, limit) : request('parts/search', {
...langParams(),
query: pn,
...limitParams(limit)
}, 'fdnext.result.v1', options);
};
export const summarizePartNumber = async (pn, options = {}) => summaryText(await decodePartNumber(pn, options));
export const decodeFlashId = async (id, options = {}) => {
const input = { idScheme: 'nand.flash_id' };
return useEmbeddedParser() ? decodeEmbeddedFlashId(id) : request('identifiers/decode', {
...langParams(),
query: id,
...input,
...controllerGroupParams()
}, 'fdnext.result.v1', options);
};
export const searchFlashId = async (id, limit = 0, options = {}) => {
const input = { idScheme: 'nand.flash_id' };
return useEmbeddedParser() ? searchEmbeddedFlashId(id, limit) : request('identifiers/search', {
...langParams(),
query: id,
...input,
...limitParams(limit)
}, 'fdnext.result.v1', options);
};
export const summarizeFlashId = async (id, options = {}) => summaryText(await decodeFlashId(id, options));