@pikku/cli 0.7.1 → 0.7.3
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 +17 -0
- package/bin/pikku-all.ts +5 -2
- package/bin/pikku-channels-map.ts +2 -33
- package/bin/pikku-channels.ts +2 -30
- package/bin/pikku-fetch.ts +3 -3
- package/bin/pikku-function-types.ts +8 -42
- package/bin/pikku-functions.ts +3 -16
- package/bin/pikku-http-map.ts +3 -32
- package/bin/pikku-http-routes.ts +2 -30
- package/bin/pikku-nextjs.ts +1 -1
- package/bin/pikku-openapi.ts +4 -2
- package/bin/pikku-rpc-map.ts +25 -0
- package/bin/pikku-rpc.ts +62 -7
- package/bin/pikku-scheduler.ts +2 -30
- package/bin/pikku-schemas.ts +5 -4
- package/bin/pikku-websocket.ts +1 -1
- package/bin/pikku.ts +0 -10
- package/dist/bin/pikku-all.d.ts +1 -1
- package/dist/bin/pikku-all.js +5 -2
- package/dist/bin/pikku-channels-map.d.ts +0 -2
- package/dist/bin/pikku-channels-map.js +1 -16
- package/dist/bin/pikku-channels.d.ts +0 -2
- package/dist/bin/pikku-channels.js +1 -16
- package/dist/bin/pikku-fetch.d.ts +1 -1
- package/dist/bin/pikku-fetch.js +1 -1
- package/dist/bin/pikku-function-types.d.ts +2 -4
- package/dist/bin/pikku-function-types.js +3 -22
- package/dist/bin/pikku-functions.js +3 -4
- package/dist/bin/pikku-http-map.d.ts +0 -2
- package/dist/bin/pikku-http-map.js +2 -17
- package/dist/bin/pikku-http-routes.d.ts +0 -2
- package/dist/bin/pikku-http-routes.js +1 -16
- package/dist/bin/pikku-nextjs.d.ts +1 -1
- package/dist/bin/pikku-nextjs.js +1 -1
- package/dist/bin/pikku-openapi.js +3 -3
- package/dist/bin/pikku-rpc-map.d.ts +3 -0
- package/dist/bin/pikku-rpc-map.js +8 -0
- package/dist/bin/pikku-rpc.d.ts +5 -1
- package/dist/bin/pikku-rpc.js +33 -6
- package/dist/bin/pikku-scheduler.d.ts +0 -2
- package/dist/bin/pikku-scheduler.js +1 -16
- package/dist/bin/pikku-schemas.d.ts +1 -1
- package/dist/bin/pikku-schemas.js +4 -4
- package/dist/bin/pikku-websocket.d.ts +1 -1
- package/dist/bin/pikku-websocket.js +1 -1
- package/dist/bin/pikku.js +0 -10
- package/dist/src/inspector-glob.js +1 -1
- 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 +7 -5
- package/dist/src/pikku-cli-config.js +6 -0
- package/dist/src/schema-generator.d.ts +3 -3
- package/dist/src/schema-generator.js +16 -34
- package/dist/src/serialize-pikku-types.d.ts +1 -1
- package/dist/src/serialize-pikku-types.js +6 -3
- package/dist/src/serialize-typed-channel-map.js +2 -2
- package/dist/src/serialize-typed-http-map.d.ts +2 -1
- package/dist/src/serialize-typed-http-map.js +11 -5
- package/dist/src/serialize-typed-rpc-map.d.ts +4 -0
- package/dist/src/serialize-typed-rpc-map.js +66 -0
- package/dist/src/{utils.d.ts → utils/utils.d.ts} +2 -1
- package/dist/src/{utils.js → utils/utils.js} +15 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/inspector-glob.ts +1 -1
- package/src/openapi-spec-generator.ts +23 -4
- package/src/pikku-cli-config.ts +31 -5
- package/src/schema-generator.ts +18 -36
- package/src/serialize-pikku-types.ts +6 -3
- package/src/serialize-typed-channel-map.ts +2 -2
- package/src/serialize-typed-http-map.ts +19 -3
- package/src/serialize-typed-rpc-map.ts +105 -0
- package/src/{utils.ts → utils/utils.ts} +20 -2
- package/dist/src/serialize-typed-function-map.d.ts +0 -4
- package/dist/src/serialize-typed-function-map.js +0 -107
- package/src/serialize-typed-function-map.ts +0 -151
- /package/dist/src/{serialize-import-map.d.ts → utils/serialize-import-map.d.ts} +0 -0
- /package/dist/src/{serialize-import-map.js → utils/serialize-import-map.js} +0 -0
- /package/src/{serialize-import-map.ts → utils/serialize-import-map.ts} +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { relative, dirname } from 'path'
|
|
2
|
-
import { PathToNameAndType, InspectorState } from '@pikku/inspector'
|
|
2
|
+
import { PathToNameAndType, InspectorState, TypesMap } from '@pikku/inspector'
|
|
3
3
|
import { mkdir, writeFile } from 'fs/promises'
|
|
4
4
|
import chalk from 'chalk'
|
|
5
5
|
import { fileURLToPath } from 'url'
|
|
@@ -248,7 +248,7 @@ export const logPikkuLogo = () => {
|
|
|
248
248
|
logPrimary(logo)
|
|
249
249
|
|
|
250
250
|
const packageJson = JSON.parse(
|
|
251
|
-
readFileSync(`${dirname(__filename)}
|
|
251
|
+
readFileSync(`${dirname(__filename)}/../../../package.json`, 'utf-8')
|
|
252
252
|
)
|
|
253
253
|
logPrimary(`⚙️ Welcome to the Pikku CLI (v${packageJson.version})\n`)
|
|
254
254
|
}
|
|
@@ -282,3 +282,21 @@ export const serializeFileImports = (
|
|
|
282
282
|
|
|
283
283
|
return serializedOutput.join('\n')
|
|
284
284
|
}
|
|
285
|
+
|
|
286
|
+
export function generateCustomTypes(
|
|
287
|
+
typesMap: TypesMap,
|
|
288
|
+
requiredTypes: Set<string>
|
|
289
|
+
) {
|
|
290
|
+
return `
|
|
291
|
+
// Custom types are those that are defined directly within generics
|
|
292
|
+
// or are broken into simpler types
|
|
293
|
+
${Array.from(typesMap.customTypes.entries())
|
|
294
|
+
.map(([name, { type, references }]) => {
|
|
295
|
+
references.forEach((name) => {
|
|
296
|
+
const originalName = typesMap.getTypeMeta(name).originalName
|
|
297
|
+
requiredTypes.add(originalName)
|
|
298
|
+
})
|
|
299
|
+
return `export type ${name} = ${type}`
|
|
300
|
+
})
|
|
301
|
+
.join('\n')}`
|
|
302
|
+
}
|
|
@@ -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 './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,151 +0,0 @@
|
|
|
1
|
-
import { HTTPRoutesMeta } from '@pikku/core/http'
|
|
2
|
-
import { serializeImportMap } from './serialize-import-map.js'
|
|
3
|
-
import { MetaInputTypes, TypesMap } from '@pikku/inspector'
|
|
4
|
-
|
|
5
|
-
export const serializeTypedRoutesMap = (
|
|
6
|
-
relativeToPath: string,
|
|
7
|
-
packageMappings: Record<string, string>,
|
|
8
|
-
typesMap: TypesMap,
|
|
9
|
-
routesMeta: HTTPRoutesMeta,
|
|
10
|
-
metaTypes: MetaInputTypes
|
|
11
|
-
) => {
|
|
12
|
-
const requiredTypes = new Set<string>()
|
|
13
|
-
const serializedCustomTypes = generateCustomTypes(typesMap, requiredTypes)
|
|
14
|
-
const serializedMetaTypes = generateMetaTypes(metaTypes, typesMap)
|
|
15
|
-
const serializedRoutes = generateRoutes(routesMeta, typesMap, requiredTypes)
|
|
16
|
-
|
|
17
|
-
const serializedImportMap = serializeImportMap(
|
|
18
|
-
relativeToPath,
|
|
19
|
-
packageMappings,
|
|
20
|
-
typesMap,
|
|
21
|
-
requiredTypes
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
return `/**
|
|
25
|
-
* This provides the structure needed for typescript to be aware of routes and their return types
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
${serializedImportMap}
|
|
29
|
-
${serializedCustomTypes}
|
|
30
|
-
${serializedMetaTypes}
|
|
31
|
-
|
|
32
|
-
interface RouteHandler<I, O> {
|
|
33
|
-
input: I;
|
|
34
|
-
output: O;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
${serializedRoutes}
|
|
38
|
-
|
|
39
|
-
export type RouteHandlerOf<Route extends keyof RoutesMap, Method extends keyof RoutesMap[Route]> =
|
|
40
|
-
RoutesMap[Route][Method] extends { input: infer I; output: infer O }
|
|
41
|
-
? RouteHandler<I, O>
|
|
42
|
-
: never;
|
|
43
|
-
|
|
44
|
-
export type RoutesWithMethod<Method extends string> = {
|
|
45
|
-
[Route in keyof RoutesMap]: Method extends keyof RoutesMap[Route] ? Route : never;
|
|
46
|
-
}[keyof RoutesMap];
|
|
47
|
-
`
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function generateCustomTypes(
|
|
51
|
-
typesMap: TypesMap,
|
|
52
|
-
requiredTypes: Set<string>
|
|
53
|
-
) {
|
|
54
|
-
return `
|
|
55
|
-
// Custom types are those that are defined directly within generics
|
|
56
|
-
// or are broken into simpler types
|
|
57
|
-
${Array.from(typesMap.customTypes.entries())
|
|
58
|
-
.map(([name, { type, references }]) => {
|
|
59
|
-
references.forEach((name) => {
|
|
60
|
-
const originalName = typesMap.getTypeMeta(name).originalName
|
|
61
|
-
requiredTypes.add(originalName)
|
|
62
|
-
})
|
|
63
|
-
return `export type ${name} = ${type}`
|
|
64
|
-
})
|
|
65
|
-
.join('\n')}`
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function generateRoutes(
|
|
69
|
-
routesMeta: HTTPRoutesMeta,
|
|
70
|
-
typesMap: TypesMap,
|
|
71
|
-
requiredTypes: Set<string>
|
|
72
|
-
) {
|
|
73
|
-
// Initialize an object to collect routes
|
|
74
|
-
const routesObj: Record<
|
|
75
|
-
string,
|
|
76
|
-
Record<string, { inputType: string; outputType: string }>
|
|
77
|
-
> = {}
|
|
78
|
-
|
|
79
|
-
for (const meta of routesMeta) {
|
|
80
|
-
const { route, method, input, output } = meta
|
|
81
|
-
|
|
82
|
-
// Initialize the route entry if it doesn't exist
|
|
83
|
-
if (!routesObj[route]) {
|
|
84
|
-
routesObj[route] = {}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Store the input and output types separately for RouteHandler
|
|
88
|
-
const inputType = input ? typesMap.getTypeMeta(input).uniqueName : 'null'
|
|
89
|
-
const outputType = output ? typesMap.getTypeMeta(output).uniqueName : 'null'
|
|
90
|
-
|
|
91
|
-
requiredTypes.add(inputType)
|
|
92
|
-
requiredTypes.add(outputType)
|
|
93
|
-
|
|
94
|
-
// Add method entry
|
|
95
|
-
routesObj[route][method] = {
|
|
96
|
-
inputType,
|
|
97
|
-
outputType,
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// Build the routes object as a string
|
|
102
|
-
let routesStr = 'export type RoutesMap = {\n'
|
|
103
|
-
|
|
104
|
-
for (const [routePath, methods] of Object.entries(routesObj)) {
|
|
105
|
-
routesStr += ` readonly '${routePath}': {\n`
|
|
106
|
-
for (const [method, handler] of Object.entries(methods)) {
|
|
107
|
-
routesStr += ` readonly ${method.toUpperCase()}: RouteHandler<${handler.inputType}, ${handler.outputType}>,\n`
|
|
108
|
-
}
|
|
109
|
-
routesStr += ' },\n'
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
routesStr += '};'
|
|
113
|
-
|
|
114
|
-
return routesStr
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const generateMetaTypes = (metaTypes: MetaInputTypes, typesMap: TypesMap) => {
|
|
118
|
-
const nameToTypeMap = Array.from(metaTypes.entries()).reduce<
|
|
119
|
-
Map<string, string>
|
|
120
|
-
>((result, [_name, { query, body, params }]) => {
|
|
121
|
-
const { uniqueName } = typesMap.getTypeMeta(_name)
|
|
122
|
-
const queryType =
|
|
123
|
-
query && query.length > 0
|
|
124
|
-
? `Pick<${uniqueName}, '${query?.join("' | '")}'>`
|
|
125
|
-
: undefined
|
|
126
|
-
if (queryType) {
|
|
127
|
-
result.set(`${uniqueName}Query`, queryType)
|
|
128
|
-
}
|
|
129
|
-
const paramsType =
|
|
130
|
-
params && params.length > 0
|
|
131
|
-
? `Pick<${uniqueName}, '${params.join("' | '")}'>`
|
|
132
|
-
: undefined
|
|
133
|
-
if (paramsType) {
|
|
134
|
-
result.set(`${uniqueName}Params`, paramsType)
|
|
135
|
-
}
|
|
136
|
-
const bodyType =
|
|
137
|
-
(body && body.length > 0) || (params && params.length > 0)
|
|
138
|
-
? `Omit<${uniqueName}, '${[...new Set([...(query || []), ...(params || [])])].join("' | '")}'>`
|
|
139
|
-
: uniqueName!
|
|
140
|
-
if (bodyType) {
|
|
141
|
-
result.set(`${uniqueName}Body`, bodyType)
|
|
142
|
-
}
|
|
143
|
-
return result
|
|
144
|
-
}, new Map())
|
|
145
|
-
|
|
146
|
-
return `
|
|
147
|
-
// The '& {}' is a workaround for not directly refering to a type since it confuses typescript
|
|
148
|
-
${Array.from(nameToTypeMap.entries())
|
|
149
|
-
.map(([name, type]) => `export type ${name} = ${type} & {}`)
|
|
150
|
-
.join('\n')}`
|
|
151
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|