MCPcopy
hub / github.com/jef/streetmerchant / handleAPI

Function handleAPI

src/web/index.ts:93–175  ·  view source on GitHub ↗
(
  request: IncomingMessage,
  response: ServerResponse,
  urlComponents: string[]
)

Source from the content-addressed store, hash-verified

91}
92
93function handleAPI(
94 request: IncomingMessage,
95 response: ServerResponse,
96 urlComponents: string[]
97) {
98 if (urlComponents.length < 2) {
99 sendError(response, 'No API route specified', 400);
100 return;
101 }
102
103 switch (urlComponents[1]) {
104 case 'config':
105 if (request.method === 'PUT') {
106 const data: string[] = [];
107 request.on('data', chunk => {
108 data.push(chunk);
109 });
110 request.on('end', () => {
111 // We ignore errors, client just sent wrong data...
112 try {
113 setConfig(JSON.parse(data.join('')));
114 updateStores();
115 } catch {
116 logger.warn('Could not setup config');
117 }
118
119 sendConfig(response);
120 });
121 return;
122 }
123
124 sendConfig(response);
125 return;
126 case 'stores':
127 sendJSON(response, [...storeList.keys()]);
128 return;
129 case 'countries':
130 sendJSON(response, getAllCountries());
131 return;
132 case 'brands':
133 sendJSON(response, getAllBrands());
134 return;
135 case 'series':
136 sendJSON(response, getAllSeries());
137 return;
138 case 'models':
139 sendJSON(response, getAllModels());
140 return;
141 case 'screenshots':
142 if (urlComponents.length >= 3) {
143 const timeStamp = urlComponents[2];
144 if (/\D/.test(timeStamp)) {
145 sendError(response, 'Invalid screenshot timestamp', 400);
146 return;
147 }
148
149 sendFile(response, `success-${timeStamp}.png`, screenshotDir);
150 return;

Callers 1

requestListenerFunction · 0.85

Calls 10

setConfigFunction · 0.90
updateStoresFunction · 0.90
getAllCountriesFunction · 0.90
getAllBrandsFunction · 0.90
getAllSeriesFunction · 0.90
getAllModelsFunction · 0.90
sendErrorFunction · 0.85
sendConfigFunction · 0.85
sendJSONFunction · 0.85
sendFileFunction · 0.85

Tested by

no test coverage detected