@pikku/cli 0.7.0 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/bin/pikku-all.ts +203 -0
- package/bin/pikku-channels-map.ts +55 -0
- package/bin/pikku-channels.ts +63 -0
- package/bin/pikku-fetch.ts +55 -0
- package/bin/pikku-function-types.ts +84 -0
- package/bin/pikku-functions.ts +35 -0
- package/bin/pikku-http-map.ts +56 -0
- package/bin/pikku-http-routes.ts +63 -0
- package/bin/pikku-nextjs.test.ts +279 -0
- package/bin/pikku-nextjs.ts +152 -0
- package/bin/pikku-openapi.ts +76 -0
- package/bin/pikku-rpc.ts +22 -0
- package/bin/pikku-scheduler.ts +64 -0
- package/bin/pikku-schemas.ts +57 -0
- package/bin/pikku-websocket.ts +58 -0
- package/bin/pikku.ts +26 -0
- package/dist/bin/pikku-all.js +3 -0
- package/dist/bin/pikku-functions.d.ts +0 -2
- package/dist/bin/pikku-functions.js +2 -17
- package/dist/bin/pikku-http-map.js +1 -1
- package/dist/bin/pikku-openapi.js +2 -2
- package/dist/bin/pikku-rpc.d.ts +3 -0
- package/dist/bin/pikku-rpc.js +8 -0
- package/dist/bin/pikku-schemas.js +2 -2
- package/dist/bin/pikku.js +0 -0
- package/dist/src/openapi-spec-generator.d.ts +2 -2
- package/dist/src/openapi-spec-generator.js +14 -5
- package/dist/src/pikku-cli-config.d.ts +1 -0
- package/dist/src/pikku-cli-config.js +3 -0
- package/dist/src/schema-generator.d.ts +3 -3
- package/dist/src/schema-generator.js +15 -34
- package/dist/src/serialize-typed-function-map.d.ts +2 -1
- package/dist/src/serialize-typed-function-map.js +6 -4
- package/dist/src/serialize-typed-http-map.d.ts +2 -1
- package/dist/src/serialize-typed-http-map.js +10 -4
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +3 -3
- package/src/inspector-glob.ts +28 -0
- package/src/openapi-spec-generator.ts +246 -0
- package/src/pikku-cli-config.ts +240 -0
- package/src/schema-generator.ts +118 -0
- package/{dist/src/events/http/serialize-fetch-wrapper.js → src/serialize-fetch-wrapper.ts} +4 -4
- package/src/serialize-import-map.ts +34 -0
- package/{dist/src/nextjs/serialize-nextjs-backend-wrapper.js → src/serialize-nextjs-backend-wrapper.ts} +11 -4
- package/{dist/src/nextjs/serialize-nextjs-http-wrapper.js → src/serialize-nextjs-http-wrapper.ts} +7 -4
- package/{dist/src/core/serialize-pikku-types.js → src/serialize-pikku-types.ts} +46 -35
- package/src/serialize-scheduler-meta.ts +18 -0
- package/src/serialize-typed-channel-map.ts +138 -0
- package/src/serialize-typed-function-map.ts +161 -0
- package/src/serialize-typed-http-map.ts +167 -0
- package/{dist/src/channels/serialize-websocket-wrapper.js → src/serialize-websocket-wrapper.ts} +4 -4
- package/src/utils.ts +284 -0
- package/tsconfig.json +21 -0
- package/dist/bin/pikku-http.d.ts +0 -5
- package/dist/bin/pikku-http.js +0 -27
- package/dist/bin/pikku-routes-map.d.ts +0 -5
- package/dist/bin/pikku-routes-map.js +0 -23
- package/dist/src/channels/serialize-channels.d.ts +0 -3
- package/dist/src/channels/serialize-channels.js +0 -19
- package/dist/src/channels/serialize-typed-channel-map.d.ts +0 -3
- package/dist/src/channels/serialize-typed-channel-map.js +0 -93
- package/dist/src/channels/serialize-websocket-wrapper.d.ts +0 -1
- package/dist/src/core/serialize-import-map.d.ts +0 -2
- package/dist/src/core/serialize-import-map.js +0 -24
- package/dist/src/core/serialize-pikku-types.d.ts +0 -4
- package/dist/src/events/channels/serialize-channels.d.ts +0 -3
- package/dist/src/events/channels/serialize-channels.js +0 -19
- package/dist/src/events/channels/serialize-typed-channel-map.d.ts +0 -3
- package/dist/src/events/channels/serialize-typed-channel-map.js +0 -90
- package/dist/src/events/channels/serialize-websocket-wrapper.d.ts +0 -1
- package/dist/src/events/channels/serialize-websocket-wrapper.js +0 -61
- package/dist/src/events/http/serialize-fetch-wrapper.d.ts +0 -1
- package/dist/src/events/http/serialize-route-imports.d.ts +0 -1
- package/dist/src/events/http/serialize-route-imports.js +0 -13
- package/dist/src/events/http/serialize-route-meta.d.ts +0 -2
- package/dist/src/events/http/serialize-route-meta.js +0 -6
- package/dist/src/events/http/serialize-typed-route-map.d.ts +0 -4
- package/dist/src/events/http/serialize-typed-route-map.js +0 -107
- package/dist/src/events/scheduler/serialize-schedulers.d.ts +0 -3
- package/dist/src/events/scheduler/serialize-schedulers.js +0 -23
- package/dist/src/http/serialize-fetch-wrapper.d.ts +0 -1
- package/dist/src/http/serialize-fetch-wrapper.js +0 -67
- package/dist/src/http/serialize-route-imports.d.ts +0 -1
- package/dist/src/http/serialize-route-imports.js +0 -13
- package/dist/src/http/serialize-route-meta.d.ts +0 -2
- package/dist/src/http/serialize-route-meta.js +0 -6
- package/dist/src/http/serialize-typed-route-map.d.ts +0 -4
- package/dist/src/http/serialize-typed-route-map.js +0 -107
- package/dist/src/nextjs/serialize-nextjs-backend-wrapper.d.ts +0 -1
- package/dist/src/nextjs/serialize-nextjs-http-wrapper.d.ts +0 -1
- package/dist/src/openapi/openapi-spec-generator.d.ts +0 -79
- package/dist/src/openapi/openapi-spec-generator.js +0 -136
- package/dist/src/scheduler/serialize-schedulers.d.ts +0 -3
- package/dist/src/scheduler/serialize-schedulers.js +0 -23
- package/dist/src/schema/schema-generator.d.ts +0 -5
- package/dist/src/schema/schema-generator.js +0 -89
- package/dist/src/serialize-typed-route-map.d.ts +0 -4
- package/dist/src/serialize-typed-route-map.js +0 -107
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
export const serializeWebsocketWrapper = (channelsMapPath) => {
|
|
2
|
-
return `import { CorePikkuWebsocket, CorePikkuRouteHandler } from '@pikku/websocket'
|
|
3
|
-
import { ChannelDefaultHandlerOf, ChannelRouteHandlerOf, ChannelsMap } from '${channelsMapPath}';
|
|
4
|
-
|
|
5
|
-
class PikkuWebSocketRoute<Channel extends keyof ChannelsMap, Route extends keyof ChannelsMap[Channel]['routes']> extends CorePikkuRouteHandler {
|
|
6
|
-
public subscribe<
|
|
7
|
-
Method extends keyof ChannelsMap[Channel]['routes'][Route],
|
|
8
|
-
Data extends ChannelRouteHandlerOf<Channel, Route, Method>['output']
|
|
9
|
-
>(method: Method, callback: (data: Data) => void
|
|
10
|
-
) {
|
|
11
|
-
super.subscribe(method.toString(), callback)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public unsubscribe<
|
|
15
|
-
Method extends keyof ChannelsMap[Channel]['routes'][Route],
|
|
16
|
-
Data extends ChannelRouteHandlerOf<Channel, Route, Method>['output']
|
|
17
|
-
>(method: Method, callback?: (data: Data) => void) {
|
|
18
|
-
super.unsubscribe(method.toString(), callback)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public send<
|
|
22
|
-
Method extends keyof ChannelsMap[Channel]['routes'][Route],
|
|
23
|
-
Data extends ChannelRouteHandlerOf<Channel, Route, Method>['input']
|
|
24
|
-
>(method: Method, data: Data) {
|
|
25
|
-
super.send(method.toString(), data)
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export class PikkuWebSocket<Channel extends keyof ChannelsMap> extends CorePikkuWebsocket {
|
|
30
|
-
/**
|
|
31
|
-
* Send a message to a specific route and method.
|
|
32
|
-
* Validates the input data type.
|
|
33
|
-
*/
|
|
34
|
-
public getRoute<Route extends keyof ChannelsMap[Channel]['routes']>(route: Route): PikkuWebSocketRoute<Channel, Route> {
|
|
35
|
-
return super.getRoute(route)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Subscribe to a specific route and method.
|
|
40
|
-
*/
|
|
41
|
-
public subscribe<Data extends ChannelDefaultHandlerOf<Channel>['output']>(
|
|
42
|
-
callback: (data: Data) => void
|
|
43
|
-
) {
|
|
44
|
-
super.subscribe(callback)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Subscribe to a specific route and method.
|
|
49
|
-
*/
|
|
50
|
-
public unsubscribe<Data extends ChannelDefaultHandlerOf<Channel>['output']>(
|
|
51
|
-
callback?: (data: Data) => void
|
|
52
|
-
) {
|
|
53
|
-
super.unsubscribe(callback)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
public send(data: ChannelDefaultHandlerOf<Channel>['input']) {
|
|
57
|
-
super.send(data)
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
`;
|
|
61
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const serializeFetchWrapper: (routesMapPath: string) => string;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const serializeRoutes: (outputPath: string, filesWithRoutes: Set<string>, packageMappings?: Record<string, string>) => string;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { getFileImportRelativePath } from '../utils.js';
|
|
2
|
-
export const serializeRoutes = (outputPath, filesWithRoutes, packageMappings = {}) => {
|
|
3
|
-
const serializedOutput = [
|
|
4
|
-
'/* The files with an addRoute function call */',
|
|
5
|
-
];
|
|
6
|
-
Array.from(filesWithRoutes)
|
|
7
|
-
.sort()
|
|
8
|
-
.forEach((path) => {
|
|
9
|
-
const filePath = getFileImportRelativePath(outputPath, path, packageMappings);
|
|
10
|
-
serializedOutput.push(`import '${filePath}'`);
|
|
11
|
-
});
|
|
12
|
-
return serializedOutput.join('\n');
|
|
13
|
-
};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export const serializeHTTPRoutesMeta = (routesMeta) => {
|
|
2
|
-
const serializedOutput = [];
|
|
3
|
-
serializedOutput.push("import { pikkuState } from '@pikku/core'");
|
|
4
|
-
serializedOutput.push(`pikkuState('http', 'meta', ${JSON.stringify(routesMeta, null, 2)})`);
|
|
5
|
-
return serializedOutput.join('\n');
|
|
6
|
-
};
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { HTTPRoutesMeta } from '@pikku/core/http';
|
|
2
|
-
import { MetaInputTypes, TypesMap } from '@pikku/inspector';
|
|
3
|
-
export declare const serializeTypedRoutesMap: (relativeToPath: string, packageMappings: Record<string, string>, typesMap: TypesMap, routesMeta: HTTPRoutesMeta, metaTypes: MetaInputTypes) => string;
|
|
4
|
-
export declare function generateCustomTypes(typesMap: TypesMap, requiredTypes: Set<string>): string;
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import { serializeImportMap } from '../core/serialize-import-map.js';
|
|
2
|
-
export const serializeTypedRoutesMap = (relativeToPath, packageMappings, typesMap, routesMeta, metaTypes) => {
|
|
3
|
-
const requiredTypes = new Set();
|
|
4
|
-
const serializedCustomTypes = generateCustomTypes(typesMap, requiredTypes);
|
|
5
|
-
const serializedMetaTypes = generateMetaTypes(metaTypes, typesMap);
|
|
6
|
-
const serializedRoutes = generateRoutes(routesMeta, typesMap, requiredTypes);
|
|
7
|
-
const serializedImportMap = serializeImportMap(relativeToPath, packageMappings, typesMap, requiredTypes);
|
|
8
|
-
return `/**
|
|
9
|
-
* This provides the structure needed for typescript to be aware of routes and their return types
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
${serializedImportMap}
|
|
13
|
-
${serializedCustomTypes}
|
|
14
|
-
${serializedMetaTypes}
|
|
15
|
-
|
|
16
|
-
interface RouteHandler<I, O> {
|
|
17
|
-
input: I;
|
|
18
|
-
output: O;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
${serializedRoutes}
|
|
22
|
-
|
|
23
|
-
export type RouteHandlerOf<Route extends keyof RoutesMap, Method extends keyof RoutesMap[Route]> =
|
|
24
|
-
RoutesMap[Route][Method] extends { input: infer I; output: infer O }
|
|
25
|
-
? RouteHandler<I, O>
|
|
26
|
-
: never;
|
|
27
|
-
|
|
28
|
-
export type RoutesWithMethod<Method extends string> = {
|
|
29
|
-
[Route in keyof RoutesMap]: Method extends keyof RoutesMap[Route] ? Route : never;
|
|
30
|
-
}[keyof RoutesMap];
|
|
31
|
-
`;
|
|
32
|
-
};
|
|
33
|
-
export function generateCustomTypes(typesMap, requiredTypes) {
|
|
34
|
-
return `
|
|
35
|
-
// Custom types are those that are defined directly within generics
|
|
36
|
-
// or are broken into simpler types
|
|
37
|
-
${Array.from(typesMap.customTypes.entries())
|
|
38
|
-
.map(([name, { type, references }]) => {
|
|
39
|
-
references.forEach((name) => {
|
|
40
|
-
const originalName = typesMap.getTypeMeta(name).originalName;
|
|
41
|
-
requiredTypes.add(originalName);
|
|
42
|
-
});
|
|
43
|
-
return `export type ${name} = ${type}`;
|
|
44
|
-
})
|
|
45
|
-
.join('\n')}`;
|
|
46
|
-
}
|
|
47
|
-
function generateRoutes(routesMeta, typesMap, requiredTypes) {
|
|
48
|
-
// Initialize an object to collect routes
|
|
49
|
-
const routesObj = {};
|
|
50
|
-
for (const meta of routesMeta) {
|
|
51
|
-
const { route, method, input, output } = meta;
|
|
52
|
-
// Initialize the route entry if it doesn't exist
|
|
53
|
-
if (!routesObj[route]) {
|
|
54
|
-
routesObj[route] = {};
|
|
55
|
-
}
|
|
56
|
-
// Store the input and output types separately for RouteHandler
|
|
57
|
-
const inputType = input ? typesMap.getTypeMeta(input).uniqueName : 'null';
|
|
58
|
-
const outputType = output ? typesMap.getTypeMeta(output).uniqueName : 'null';
|
|
59
|
-
requiredTypes.add(inputType);
|
|
60
|
-
requiredTypes.add(outputType);
|
|
61
|
-
// Add method entry
|
|
62
|
-
routesObj[route][method] = {
|
|
63
|
-
inputType,
|
|
64
|
-
outputType,
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
// Build the routes object as a string
|
|
68
|
-
let routesStr = 'export type RoutesMap = {\n';
|
|
69
|
-
for (const [routePath, methods] of Object.entries(routesObj)) {
|
|
70
|
-
routesStr += ` readonly '${routePath}': {\n`;
|
|
71
|
-
for (const [method, handler] of Object.entries(methods)) {
|
|
72
|
-
routesStr += ` readonly ${method.toUpperCase()}: RouteHandler<${handler.inputType}, ${handler.outputType}>,\n`;
|
|
73
|
-
}
|
|
74
|
-
routesStr += ' },\n';
|
|
75
|
-
}
|
|
76
|
-
routesStr += '};';
|
|
77
|
-
return routesStr;
|
|
78
|
-
}
|
|
79
|
-
const generateMetaTypes = (metaTypes, typesMap) => {
|
|
80
|
-
const nameToTypeMap = Array.from(metaTypes.entries()).reduce((result, [_name, { query, body, params }]) => {
|
|
81
|
-
const { uniqueName } = typesMap.getTypeMeta(_name);
|
|
82
|
-
const queryType = query && query.length > 0
|
|
83
|
-
? `Pick<${uniqueName}, '${query?.join("' | '")}'>`
|
|
84
|
-
: undefined;
|
|
85
|
-
if (queryType) {
|
|
86
|
-
result.set(`${uniqueName}Query`, queryType);
|
|
87
|
-
}
|
|
88
|
-
const paramsType = params && params.length > 0
|
|
89
|
-
? `Pick<${uniqueName}, '${params.join("' | '")}'>`
|
|
90
|
-
: undefined;
|
|
91
|
-
if (paramsType) {
|
|
92
|
-
result.set(`${uniqueName}Params`, paramsType);
|
|
93
|
-
}
|
|
94
|
-
const bodyType = (body && body.length > 0) || (params && params.length > 0)
|
|
95
|
-
? `Omit<${uniqueName}, '${[...new Set([...(query || []), ...(params || [])])].join("' | '")}'>`
|
|
96
|
-
: uniqueName;
|
|
97
|
-
if (bodyType) {
|
|
98
|
-
result.set(`${uniqueName}Body`, bodyType);
|
|
99
|
-
}
|
|
100
|
-
return result;
|
|
101
|
-
}, new Map());
|
|
102
|
-
return `
|
|
103
|
-
// The '& {}' is a workaround for not directly refering to a type since it confuses typescript
|
|
104
|
-
${Array.from(nameToTypeMap.entries())
|
|
105
|
-
.map(([name, type]) => `export type ${name} = ${type} & {}`)
|
|
106
|
-
.join('\n')}`;
|
|
107
|
-
};
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { ScheduledTasksMeta } from '@pikku/core/scheduler';
|
|
2
|
-
export declare const serializeSchedulers: (outputPath: string, filesWithScheduledTasks: Set<string>, packageMappings?: Record<string, string>) => string;
|
|
3
|
-
export declare const serializeSchedulerMeta: (scheduledTasksMeta: ScheduledTasksMeta) => string;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { getFileImportRelativePath } from '../../utils.js';
|
|
2
|
-
export const serializeSchedulers = (outputPath, filesWithScheduledTasks, packageMappings = {}) => {
|
|
3
|
-
const serializedOutput = [
|
|
4
|
-
'/* The files with an addSerializedTasks function call */',
|
|
5
|
-
];
|
|
6
|
-
Array.from(filesWithScheduledTasks)
|
|
7
|
-
.sort()
|
|
8
|
-
.forEach((path) => {
|
|
9
|
-
const filePath = getFileImportRelativePath(outputPath, path, packageMappings);
|
|
10
|
-
serializedOutput.push(`import '${filePath}'`);
|
|
11
|
-
});
|
|
12
|
-
return serializedOutput.join('\n');
|
|
13
|
-
};
|
|
14
|
-
export const serializeSchedulerMeta = (scheduledTasksMeta) => {
|
|
15
|
-
const serializedOutput = [];
|
|
16
|
-
serializedOutput.push("import { pikkuState } from '@pikku/core'");
|
|
17
|
-
serializedOutput.push(`pikkuState('scheduler', 'meta', ${JSON.stringify(scheduledTasksMeta, null, 2)})`);
|
|
18
|
-
const scheduledTasksMetaValues = Object.values(scheduledTasksMeta);
|
|
19
|
-
if (scheduledTasksMetaValues.length > 0) {
|
|
20
|
-
serializedOutput.push(`export type ScheduledTaskNames = '${scheduledTasksMetaValues.map((s) => s.name).join("' | '")}'`);
|
|
21
|
-
}
|
|
22
|
-
return serializedOutput.join('\n');
|
|
23
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const serializeFetchWrapper: (routesMapPath: string) => string;
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
export const serializeFetchWrapper = (routesMapPath) => {
|
|
2
|
-
return `
|
|
3
|
-
import { CorePikkuFetch, HTTPMethod } from '@pikku/fetch'
|
|
4
|
-
import type { RoutesMap, RouteHandlerOf, RoutesWithMethod } from '${routesMapPath}'
|
|
5
|
-
|
|
6
|
-
export class PikkuFetch extends CorePikkuFetch {
|
|
7
|
-
public async post<Route extends RoutesWithMethod<'POST'>>(
|
|
8
|
-
route: Route,
|
|
9
|
-
...args: null extends RouteHandlerOf<Route, 'POST'>['input']
|
|
10
|
-
? [data?: Exclude<RouteHandlerOf<Route, 'POST'>['input'], null>, options?: Omit<RequestInit, 'body'>]
|
|
11
|
-
: [data: RouteHandlerOf<Route, 'POST'>['input'], options?: Omit<RequestInit, 'body'>]
|
|
12
|
-
): Promise<RouteHandlerOf<Route, 'POST'>['output']> {
|
|
13
|
-
const [data, options] = args;
|
|
14
|
-
return super.api(route, 'POST', data, options);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public async get<Route extends RoutesWithMethod<'GET'>>(
|
|
18
|
-
route: Route,
|
|
19
|
-
...args: null extends RouteHandlerOf<Route, 'GET'>['input']
|
|
20
|
-
? [data?: Exclude<RouteHandlerOf<Route, 'GET'>['input'], null>, options?: Omit<RequestInit, 'body'>]
|
|
21
|
-
: [data: RouteHandlerOf<Route, 'GET'>['input'], options?: Omit<RequestInit, 'body'>]
|
|
22
|
-
): Promise<RouteHandlerOf<Route, 'GET'>['output']> {
|
|
23
|
-
const [data, options] = args;
|
|
24
|
-
return super.api(route, 'GET', data, options);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public async patch<Route extends RoutesWithMethod<'PATCH'>>(
|
|
28
|
-
route: Route,
|
|
29
|
-
...args: null extends RouteHandlerOf<Route, 'PATCH'>['input']
|
|
30
|
-
? [data?: Exclude<RouteHandlerOf<Route, 'PATCH'>['input'], null>, options?: Omit<RequestInit, 'body'>]
|
|
31
|
-
: [data: RouteHandlerOf<Route, 'PATCH'>['input'], options?: Omit<RequestInit, 'body'>]
|
|
32
|
-
): Promise<RouteHandlerOf<Route, 'PATCH'>['output']> {
|
|
33
|
-
const [data, options] = args;
|
|
34
|
-
return super.api(route, 'PATCH', data, options);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public async head<Route extends RoutesWithMethod<'HEAD'>>(
|
|
38
|
-
route: Route,
|
|
39
|
-
...args: null extends RouteHandlerOf<Route, 'HEAD'>['input']
|
|
40
|
-
? [data?: Exclude<RouteHandlerOf<Route, 'HEAD'>['input'], null>, options?: Omit<RequestInit, 'body'>]
|
|
41
|
-
: [data: RouteHandlerOf<Route, 'HEAD'>['input'], options?: Omit<RequestInit, 'body'>]
|
|
42
|
-
): Promise<RouteHandlerOf<Route, 'HEAD'>['output']> {
|
|
43
|
-
const [data, options] = args;
|
|
44
|
-
return super.api(route, 'HEAD', data, options);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
public async delete<Route extends RoutesWithMethod<'DELETE'>>(
|
|
48
|
-
route: Route,
|
|
49
|
-
...args: null extends RouteHandlerOf<Route, 'DELETE'>['input']
|
|
50
|
-
? [data?: Exclude<RouteHandlerOf<Route, 'DELETE'>['input'], null>, options?: Omit<RequestInit, 'body'>]
|
|
51
|
-
: [data: RouteHandlerOf<Route, 'DELETE'>['input'], options?: Omit<RequestInit, 'body'>]
|
|
52
|
-
): Promise<RouteHandlerOf<Route, 'DELETE'>['output']> {
|
|
53
|
-
const [data, options] = args;
|
|
54
|
-
return super.api(route, 'DELETE', data, options);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
public async fetch<
|
|
58
|
-
Route extends keyof RoutesMap,
|
|
59
|
-
Method extends keyof RoutesMap[Route]
|
|
60
|
-
>(route: Route, method: Method, data: RouteHandlerOf<Route, Method>['input'], options?: Omit<RequestInit, 'body'>): Promise<Response> {
|
|
61
|
-
return await super.fetch(route, method as HTTPMethod, data, options);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export const pikkuFetch = new PikkuFetch();
|
|
66
|
-
`;
|
|
67
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const serializeRoutes: (outputPath: string, filesWithRoutes: Set<string>, packageMappings?: Record<string, string>) => string;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { getFileImportRelativePath } from '../utils.js';
|
|
2
|
-
export const serializeRoutes = (outputPath, filesWithRoutes, packageMappings = {}) => {
|
|
3
|
-
const serializedOutput = [
|
|
4
|
-
'/* The files with an addRoute function call */',
|
|
5
|
-
];
|
|
6
|
-
Array.from(filesWithRoutes)
|
|
7
|
-
.sort()
|
|
8
|
-
.forEach((path) => {
|
|
9
|
-
const filePath = getFileImportRelativePath(outputPath, path, packageMappings);
|
|
10
|
-
serializedOutput.push(`import '${filePath}'`);
|
|
11
|
-
});
|
|
12
|
-
return serializedOutput.join('\n');
|
|
13
|
-
};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export const serializeHTTPRoutesMeta = (routesMeta) => {
|
|
2
|
-
const serializedOutput = [];
|
|
3
|
-
serializedOutput.push("import { pikkuState } from '@pikku/core'");
|
|
4
|
-
serializedOutput.push(`pikkuState('http', 'meta', ${JSON.stringify(routesMeta, null, 2)})`);
|
|
5
|
-
return serializedOutput.join('\n');
|
|
6
|
-
};
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { HTTPRoutesMeta } from '@pikku/core/http';
|
|
2
|
-
import { MetaInputTypes, TypesMap } from '@pikku/inspector';
|
|
3
|
-
export declare const serializeTypedRoutesMap: (relativeToPath: string, packageMappings: Record<string, string>, typesMap: TypesMap, routesMeta: HTTPRoutesMeta, metaTypes: MetaInputTypes) => string;
|
|
4
|
-
export declare function generateCustomTypes(typesMap: TypesMap, requiredTypes: Set<string>): string;
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import { serializeImportMap } from '../core/serialize-import-map.js';
|
|
2
|
-
export const serializeTypedRoutesMap = (relativeToPath, packageMappings, typesMap, routesMeta, metaTypes) => {
|
|
3
|
-
const requiredTypes = new Set();
|
|
4
|
-
const serializedCustomTypes = generateCustomTypes(typesMap, requiredTypes);
|
|
5
|
-
const serializedMetaTypes = generateMetaTypes(metaTypes, typesMap);
|
|
6
|
-
const serializedRoutes = generateRoutes(routesMeta, typesMap, requiredTypes);
|
|
7
|
-
const serializedImportMap = serializeImportMap(relativeToPath, packageMappings, typesMap, requiredTypes);
|
|
8
|
-
return `/**
|
|
9
|
-
* This provides the structure needed for typescript to be aware of routes and their return types
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
${serializedImportMap}
|
|
13
|
-
${serializedCustomTypes}
|
|
14
|
-
${serializedMetaTypes}
|
|
15
|
-
|
|
16
|
-
interface RouteHandler<I, O> {
|
|
17
|
-
input: I;
|
|
18
|
-
output: O;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
${serializedRoutes}
|
|
22
|
-
|
|
23
|
-
export type RouteHandlerOf<Route extends keyof RoutesMap, Method extends keyof RoutesMap[Route]> =
|
|
24
|
-
RoutesMap[Route][Method] extends { input: infer I; output: infer O }
|
|
25
|
-
? RouteHandler<I, O>
|
|
26
|
-
: never;
|
|
27
|
-
|
|
28
|
-
export type RoutesWithMethod<Method extends string> = {
|
|
29
|
-
[Route in keyof RoutesMap]: Method extends keyof RoutesMap[Route] ? Route : never;
|
|
30
|
-
}[keyof RoutesMap];
|
|
31
|
-
`;
|
|
32
|
-
};
|
|
33
|
-
export function generateCustomTypes(typesMap, requiredTypes) {
|
|
34
|
-
return `
|
|
35
|
-
// Custom types are those that are defined directly within generics
|
|
36
|
-
// or are broken into simpler types
|
|
37
|
-
${Array.from(typesMap.customTypes.entries())
|
|
38
|
-
.map(([name, { type, references }]) => {
|
|
39
|
-
references.forEach((name) => {
|
|
40
|
-
const originalName = typesMap.getTypeMeta(name).originalName;
|
|
41
|
-
requiredTypes.add(originalName);
|
|
42
|
-
});
|
|
43
|
-
return `export type ${name} = ${type}`;
|
|
44
|
-
})
|
|
45
|
-
.join('\n')}`;
|
|
46
|
-
}
|
|
47
|
-
function generateRoutes(routesMeta, typesMap, requiredTypes) {
|
|
48
|
-
// Initialize an object to collect routes
|
|
49
|
-
const routesObj = {};
|
|
50
|
-
for (const meta of routesMeta) {
|
|
51
|
-
const { route, method, input, output } = meta;
|
|
52
|
-
// Initialize the route entry if it doesn't exist
|
|
53
|
-
if (!routesObj[route]) {
|
|
54
|
-
routesObj[route] = {};
|
|
55
|
-
}
|
|
56
|
-
// Store the input and output types separately for RouteHandler
|
|
57
|
-
const inputType = input ? typesMap.getTypeMeta(input).uniqueName : 'null';
|
|
58
|
-
const outputType = output ? typesMap.getTypeMeta(output).uniqueName : 'null';
|
|
59
|
-
requiredTypes.add(inputType);
|
|
60
|
-
requiredTypes.add(outputType);
|
|
61
|
-
// Add method entry
|
|
62
|
-
routesObj[route][method] = {
|
|
63
|
-
inputType,
|
|
64
|
-
outputType,
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
// Build the routes object as a string
|
|
68
|
-
let routesStr = 'export type RoutesMap = {\n';
|
|
69
|
-
for (const [routePath, methods] of Object.entries(routesObj)) {
|
|
70
|
-
routesStr += ` readonly '${routePath}': {\n`;
|
|
71
|
-
for (const [method, handler] of Object.entries(methods)) {
|
|
72
|
-
routesStr += ` readonly ${method.toUpperCase()}: RouteHandler<${handler.inputType}, ${handler.outputType}>,\n`;
|
|
73
|
-
}
|
|
74
|
-
routesStr += ' },\n';
|
|
75
|
-
}
|
|
76
|
-
routesStr += '};';
|
|
77
|
-
return routesStr;
|
|
78
|
-
}
|
|
79
|
-
const generateMetaTypes = (metaTypes, typesMap) => {
|
|
80
|
-
const nameToTypeMap = Array.from(metaTypes.entries()).reduce((result, [_name, { query, body, params }]) => {
|
|
81
|
-
const { uniqueName } = typesMap.getTypeMeta(_name);
|
|
82
|
-
const queryType = query && query.length > 0
|
|
83
|
-
? `Pick<${uniqueName}, '${query?.join("' | '")}'>`
|
|
84
|
-
: undefined;
|
|
85
|
-
if (queryType) {
|
|
86
|
-
result.set(`${uniqueName}Query`, queryType);
|
|
87
|
-
}
|
|
88
|
-
const paramsType = params && params.length > 0
|
|
89
|
-
? `Pick<${uniqueName}, '${params.join("' | '")}'>`
|
|
90
|
-
: undefined;
|
|
91
|
-
if (paramsType) {
|
|
92
|
-
result.set(`${uniqueName}Params`, paramsType);
|
|
93
|
-
}
|
|
94
|
-
const bodyType = (body && body.length > 0) || (params && params.length > 0)
|
|
95
|
-
? `Omit<${uniqueName}, '${[...new Set([...(query || []), ...(params || [])])].join("' | '")}'>`
|
|
96
|
-
: uniqueName;
|
|
97
|
-
if (bodyType) {
|
|
98
|
-
result.set(`${uniqueName}Body`, bodyType);
|
|
99
|
-
}
|
|
100
|
-
return result;
|
|
101
|
-
}, new Map());
|
|
102
|
-
return `
|
|
103
|
-
// The '& {}' is a workaround for not directly refering to a type since it confuses typescript
|
|
104
|
-
${Array.from(nameToTypeMap.entries())
|
|
105
|
-
.map(([name, type]) => `export type ${name} = ${type} & {}`)
|
|
106
|
-
.join('\n')}`;
|
|
107
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const serializeNextJsBackendWrapper: (routesPath: string, routesMapPath: string, schemasPath: string, configImport: string, singleServicesFactoryImport: string, sessionServicesImport: string) => string;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const serializeNextJsHTTPWrapper: (routesMapPath: string, pikkuFetchImport: string) => string;
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { HTTPRoutesMeta } from '@pikku/core';
|
|
2
|
-
interface OpenAPISpec {
|
|
3
|
-
openapi: string;
|
|
4
|
-
info: {
|
|
5
|
-
title: string;
|
|
6
|
-
version: string;
|
|
7
|
-
description?: string;
|
|
8
|
-
termsOfService?: string;
|
|
9
|
-
contact?: {
|
|
10
|
-
name?: string;
|
|
11
|
-
url?: string;
|
|
12
|
-
email?: string;
|
|
13
|
-
};
|
|
14
|
-
license?: {
|
|
15
|
-
name: string;
|
|
16
|
-
url?: string;
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
servers: {
|
|
20
|
-
url: string;
|
|
21
|
-
description?: string;
|
|
22
|
-
}[];
|
|
23
|
-
paths: Record<string, any>;
|
|
24
|
-
components: {
|
|
25
|
-
schemas: Record<string, any>;
|
|
26
|
-
responses?: Record<string, any>;
|
|
27
|
-
parameters?: Record<string, any>;
|
|
28
|
-
examples?: Record<string, any>;
|
|
29
|
-
requestBodies?: Record<string, any>;
|
|
30
|
-
headers?: Record<string, any>;
|
|
31
|
-
securitySchemes?: Record<string, any>;
|
|
32
|
-
};
|
|
33
|
-
security?: {
|
|
34
|
-
[key: string]: any[];
|
|
35
|
-
}[];
|
|
36
|
-
tags?: {
|
|
37
|
-
name: string;
|
|
38
|
-
description?: string;
|
|
39
|
-
}[];
|
|
40
|
-
externalDocs?: {
|
|
41
|
-
description?: string;
|
|
42
|
-
url: string;
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
export interface OpenAPISpecInfo {
|
|
46
|
-
info: {
|
|
47
|
-
title: string;
|
|
48
|
-
version: string;
|
|
49
|
-
description: string;
|
|
50
|
-
termsOfService?: string;
|
|
51
|
-
contact?: {
|
|
52
|
-
name?: string;
|
|
53
|
-
url?: string;
|
|
54
|
-
email?: string;
|
|
55
|
-
};
|
|
56
|
-
license?: {
|
|
57
|
-
name: string;
|
|
58
|
-
url?: string;
|
|
59
|
-
};
|
|
60
|
-
};
|
|
61
|
-
servers: {
|
|
62
|
-
url: string;
|
|
63
|
-
description?: string;
|
|
64
|
-
}[];
|
|
65
|
-
tags?: {
|
|
66
|
-
name: string;
|
|
67
|
-
description?: string;
|
|
68
|
-
}[];
|
|
69
|
-
externalDocs?: {
|
|
70
|
-
description?: string;
|
|
71
|
-
url: string;
|
|
72
|
-
};
|
|
73
|
-
securitySchemes?: Record<string, any>;
|
|
74
|
-
security?: {
|
|
75
|
-
[key: string]: any[];
|
|
76
|
-
}[];
|
|
77
|
-
}
|
|
78
|
-
export declare function generateOpenAPISpec(routeMeta: HTTPRoutesMeta, schemas: Record<string, any>, additionalInfo: OpenAPISpecInfo): Promise<OpenAPISpec>;
|
|
79
|
-
export {};
|