-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathflashApi.js
More file actions
119 lines (104 loc) · 3.72 KB
/
flashApi.js
File metadata and controls
119 lines (104 loc) · 3.72 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
import store from '@/store';
import {
decodeEmbeddedFlashId,
decodeEmbeddedPartNumber,
getEmbeddedInfo,
searchEmbeddedFlashId,
searchEmbeddedPartNumber,
summarizeEmbeddedFlashId,
summarizeEmbeddedPartNumber
} from '@/services/fdnextApi';
import { FDNEXT_CAPABILITIES_SCHEMA_VERSIONS, summaryText } from '@/services/fdnextResultView';
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') => {
const response = await fetch(makeUrl(endpoint, params));
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}`);
};
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 () => useEmbeddedParser()
? getEmbeddedInfo()
: request('capabilities', langParams(), FDNEXT_CAPABILITIES_SCHEMA_VERSIONS);
export const decodePartNumber = async pn => {
return useEmbeddedParser() ? decodeEmbeddedPartNumber(pn) : request('parts/decode', {
...langParams(),
...controllerGroupParams(),
query: pn
});
};
export const searchPartNumber = async (pn, limit = 0) => {
return useEmbeddedParser() ? searchEmbeddedPartNumber(pn, limit) : request('parts/search', {
...langParams(),
query: pn,
...controllerGroupParams(),
...limitParams(limit)
});
};
export const summarizePartNumber = async pn => useEmbeddedParser()
? summarizeEmbeddedPartNumber(pn)
: summaryText(await decodePartNumber(pn));
export const decodeFlashId = async id => {
const input = { idScheme: 'nand.flash_id' };
return useEmbeddedParser() ? decodeEmbeddedFlashId(id) : request('identifiers/decode', {
...langParams(),
query: id,
...input,
...controllerGroupParams()
});
};
export const searchFlashId = async (id, limit = 0) => {
const input = { idScheme: 'nand.flash_id' };
return useEmbeddedParser() ? searchEmbeddedFlashId(id, limit) : request('identifiers/search', {
...langParams(),
query: id,
...input,
...controllerGroupParams(),
...limitParams(limit)
});
};
export const summarizeFlashId = async id => useEmbeddedParser()
? summarizeEmbeddedFlashId(id)
: summaryText(await decodeFlashId(id));