|
1 | 1 | import {
|
2 |
| - ActionGroup, |
3 |
| - MiskActions, |
4 | 2 | MiskMetadataResponse,
|
5 |
| - MiskWebActionDefinition, |
| 3 | + MiskRoute, |
6 | 4 | } from '@web-actions/api/responseTypes';
|
7 | 5 | import { fetchCached } from '@web-actions/network/http';
|
8 | 6 | import MetadataClient from '@web-actions/api/MetadataClient';
|
9 |
| - |
10 |
| -function containsJsonOrAny(mediaType: string[]) { |
11 |
| - return ( |
12 |
| - mediaType.length == 0 || |
13 |
| - mediaType.some((it) => it.startsWith('application/json')) || |
14 |
| - mediaType.some((it) => it.startsWith('*/*')) |
15 |
| - ); |
16 |
| -} |
| 7 | +import { buildRoutes } from 'src/web-actions/api/BuildRoutes'; |
17 | 8 |
|
18 | 9 | export default class RealMetadataClient implements MetadataClient {
|
19 |
| - async fetchMetadata(): Promise<MiskActions> { |
20 |
| - const response = await fetchCached<MiskMetadataResponse>( |
21 |
| - `/api/web-actions/metadata`, |
| 10 | + async fetchMetadata(): Promise<MiskRoute[]> { |
| 11 | + return fetchCached<MiskMetadataResponse>(`/api/web-actions/metadata`).then( |
| 12 | + (it) => buildRoutes(it.all['web-actions'].metadata), |
22 | 13 | );
|
23 |
| - const actionMap: Record<string, ActionGroup> = {}; |
24 |
| - |
25 |
| - response.all['web-actions'].metadata.forEach((it) => { |
26 |
| - it.requestType = it.requestType === 'null' ? null : it.requestType; |
27 |
| - |
28 |
| - const qualifiedName = `${it.packageName}.${it.name}`; |
29 |
| - const groupKey = `${it.httpMethod} ${it.pathPattern} ${qualifiedName}`; |
30 |
| - let group = actionMap[groupKey]; |
31 |
| - |
32 |
| - if (group === undefined) { |
33 |
| - group = { |
34 |
| - actionName: qualifiedName, |
35 |
| - path: it.pathPattern, |
36 |
| - httpMethod: it.httpMethod || '', |
37 |
| - callables: [], |
38 |
| - all: [], |
39 |
| - responseMediaTypes: [], |
40 |
| - requestMediaTypes: [], |
41 |
| - canCall: false, |
42 |
| - types: it.types, |
43 |
| - requestType: it.requestType, |
44 |
| - }; |
45 |
| - actionMap[groupKey] = group; |
46 |
| - } |
47 |
| - |
48 |
| - if (it.httpMethod === 'GET' || containsJsonOrAny(it.requestMediaTypes)) { |
49 |
| - group.canCall = true; |
50 |
| - group.callables.push(it); |
51 |
| - } |
52 |
| - |
53 |
| - group.requestMediaTypes.push(...it.requestMediaTypes); |
54 |
| - group.responseMediaTypes.push(it.responseMediaType); |
55 |
| - |
56 |
| - group.all.push(it); |
57 |
| - }); |
58 |
| - |
59 |
| - return actionMap; |
60 | 14 | }
|
61 | 15 | }
|
0 commit comments