tspace-spear 1.2.6 → 1.2.8
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/README.md +101 -105
- package/dist/cli/app.d.ts +1 -1
- package/dist/cli/app.js +12 -4
- package/dist/cli/app.js.map +1 -1
- package/dist/cli/client.d.ts +1 -0
- package/dist/cli/client.js +30 -0
- package/dist/cli/client.js.map +1 -0
- package/dist/cli/controller.d.ts +1 -1
- package/dist/cli/controller.js +12 -1
- package/dist/cli/controller.js.map +1 -1
- package/dist/cli/generators/app/index.d.ts +1 -0
- package/dist/cli/generators/app/index.js +124 -0
- package/dist/cli/generators/app/index.js.map +1 -0
- package/dist/cli/generators/app/template.d.ts +1 -0
- package/dist/cli/generators/app/template.js +42 -0
- package/dist/cli/generators/app/template.js.map +1 -0
- package/dist/cli/generators/client/template.d.ts +1 -0
- package/dist/cli/generators/client/template.js +30 -0
- package/dist/cli/generators/client/template.js.map +1 -0
- package/dist/cli/generators/controller/index.d.ts +1 -0
- package/dist/cli/generators/controller/index.js +78 -0
- package/dist/cli/generators/controller/index.js.map +1 -0
- package/dist/cli/generators/controller/template.d.ts +1 -0
- package/dist/cli/generators/controller/template.js +99 -0
- package/dist/cli/generators/controller/template.js.map +1 -0
- package/dist/cli/generators/dto/index.d.ts +1 -0
- package/dist/cli/generators/dto/index.js +55 -0
- package/dist/cli/generators/dto/index.js.map +1 -0
- package/dist/cli/generators/dto/template.d.ts +1 -0
- package/dist/cli/generators/dto/template.js +32 -0
- package/dist/cli/generators/dto/template.js.map +1 -0
- package/dist/cli/generators/middleware/index.d.ts +1 -0
- package/dist/cli/generators/middleware/index.js +38 -0
- package/dist/cli/generators/middleware/index.js.map +1 -0
- package/dist/cli/generators/middleware/template.d.ts +1 -0
- package/dist/cli/generators/middleware/template.js +16 -0
- package/dist/cli/generators/middleware/template.js.map +1 -0
- package/dist/cli/generators/module/index.d.ts +1 -0
- package/dist/cli/generators/module/index.js +173 -0
- package/dist/cli/generators/module/index.js.map +1 -0
- package/dist/cli/generators/service/index.d.ts +1 -0
- package/dist/cli/generators/service/index.js +53 -0
- package/dist/cli/generators/service/index.js.map +1 -0
- package/dist/cli/generators/service/template.d.ts +1 -0
- package/dist/cli/generators/service/template.js +74 -0
- package/dist/cli/generators/service/template.js.map +1 -0
- package/dist/cli/generators/shared/index.d.ts +3 -0
- package/dist/cli/generators/shared/index.js +22 -0
- package/dist/cli/generators/shared/index.js.map +1 -0
- package/dist/cli/index.js +52 -125
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/middleware.d.ts +1 -0
- package/dist/cli/middleware.js +16 -0
- package/dist/cli/middleware.js.map +1 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +17 -0
- package/dist/client.js.map +1 -0
- package/dist/common/middlewares/log.middleware.d.ts +2 -0
- package/dist/common/middlewares/log.middleware.js +12 -0
- package/dist/common/middlewares/log.middleware.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/core/compiler/generator.js +37 -1
- package/dist/lib/core/compiler/generator.js.map +1 -1
- package/dist/lib/core/decorators/context.d.ts +78 -8
- package/dist/lib/core/decorators/context.js +74 -7
- package/dist/lib/core/decorators/context.js.map +1 -1
- package/dist/lib/core/package/index.d.ts +11 -0
- package/dist/lib/core/package/index.js +42 -0
- package/dist/lib/core/package/index.js.map +1 -0
- package/dist/modules/cats/cat.controller.d.ts +58 -0
- package/dist/modules/cats/cat.controller.js +90 -0
- package/dist/modules/cats/cat.controller.js.map +1 -0
- package/dist/modules/cats/cat.dto.d.ts +8 -0
- package/dist/modules/cats/cat.dto.js +44 -0
- package/dist/modules/cats/cat.dto.js.map +1 -0
- package/dist/modules/cats/cat.service.d.ts +23 -0
- package/dist/modules/cats/cat.service.js +56 -0
- package/dist/modules/cats/cat.service.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createModule = createModule;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const shared_1 = require("../shared");
|
|
10
|
+
function createModule(root, name) {
|
|
11
|
+
if (!name) {
|
|
12
|
+
console.log("Missing module path, try spear g module dogs");
|
|
13
|
+
process.exit(1);
|
|
14
|
+
}
|
|
15
|
+
const modulePath = path_1.default.resolve(process.cwd(), root, 'modules', (0, shared_1.toPlural)(name));
|
|
16
|
+
fs_1.default.mkdirSync(modulePath, { recursive: true });
|
|
17
|
+
const controllerPath = path_1.default.resolve(process.cwd(), root, "modules", (0, shared_1.toPlural)(name), `${(0, shared_1.toSingular)(name)}.controller.ts`);
|
|
18
|
+
const servicePath = path_1.default.resolve(process.cwd(), root, "modules", (0, shared_1.toPlural)(name), `${(0, shared_1.toSingular)(name)}.service.ts`);
|
|
19
|
+
const dtoPath = path_1.default.resolve(process.cwd(), root, "modules", (0, shared_1.toPlural)(name), `${(0, shared_1.toSingular)(name)}.dto.ts`);
|
|
20
|
+
const controllerName = (0, shared_1.capitalize)((0, shared_1.toSingular)(name)) + "Controller";
|
|
21
|
+
const serviceName = (0, shared_1.capitalize)((0, shared_1.toSingular)(name)) + "Service";
|
|
22
|
+
const dtoName = (0, shared_1.capitalize)((0, shared_1.toSingular)(name)) + "Dto";
|
|
23
|
+
fs_1.default.writeFileSync(controllerPath, `import {
|
|
24
|
+
type T,
|
|
25
|
+
Controller,
|
|
26
|
+
Get,
|
|
27
|
+
Post,
|
|
28
|
+
Put,
|
|
29
|
+
Patch,
|
|
30
|
+
Delete,
|
|
31
|
+
ValidateDto
|
|
32
|
+
} from "tspace-spear";
|
|
33
|
+
|
|
34
|
+
import { ${serviceName} } from "./${(0, shared_1.toSingular)(name)}.service";
|
|
35
|
+
|
|
36
|
+
import {
|
|
37
|
+
Create${dtoName},
|
|
38
|
+
Update${dtoName}
|
|
39
|
+
} from "./${(0, shared_1.toSingular)(name)}.dto";
|
|
40
|
+
|
|
41
|
+
@Controller("/${name}")
|
|
42
|
+
class ${controllerName} {
|
|
43
|
+
|
|
44
|
+
constructor(
|
|
45
|
+
private ${(0, shared_1.toSingular)(name)}Service: ${serviceName} = new ${serviceName}()
|
|
46
|
+
) {}
|
|
47
|
+
|
|
48
|
+
@Get("/")
|
|
49
|
+
async index() {
|
|
50
|
+
return this.${(0, shared_1.toSingular)(name)}Service.index();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@Get("/:id")
|
|
54
|
+
async show({
|
|
55
|
+
params
|
|
56
|
+
}: T.Context<{ params: { id: number } }>) {
|
|
57
|
+
return this.${(0, shared_1.toSingular)(name)}Service.show(params.id);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@Post("/")
|
|
61
|
+
@ValidateDto(Create${dtoName})
|
|
62
|
+
async create({
|
|
63
|
+
body
|
|
64
|
+
}: T.Context<{ body: Create${dtoName} }>) {
|
|
65
|
+
return this.${(0, shared_1.toSingular)(name)}Service
|
|
66
|
+
.create({
|
|
67
|
+
name: body.name,
|
|
68
|
+
age: body.age
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
@Put("/:id")
|
|
73
|
+
@Patch("/:id")
|
|
74
|
+
@ValidateDto(Update${dtoName})
|
|
75
|
+
async update({
|
|
76
|
+
params,
|
|
77
|
+
body
|
|
78
|
+
}: T.Context<{
|
|
79
|
+
params: { id: number };
|
|
80
|
+
body: Update${dtoName};
|
|
81
|
+
}>) {
|
|
82
|
+
|
|
83
|
+
return this.${(0, shared_1.toSingular)(name)}Service
|
|
84
|
+
.update(params.id, {
|
|
85
|
+
name: body.name,
|
|
86
|
+
age: body.age
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
@Delete("/:id")
|
|
91
|
+
async remove({
|
|
92
|
+
params
|
|
93
|
+
}: T.Context<{ params: { id: number } }>) {
|
|
94
|
+
return this.${(0, shared_1.toSingular)(name)}Service
|
|
95
|
+
.remove(params.id);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export { ${controllerName} };
|
|
100
|
+
export default ${controllerName};
|
|
101
|
+
`);
|
|
102
|
+
fs_1.default.writeFileSync(servicePath, `import {
|
|
103
|
+
Create${dtoName},
|
|
104
|
+
Update${dtoName}
|
|
105
|
+
} from "./${(0, shared_1.toSingular)(name)}.dto";
|
|
106
|
+
|
|
107
|
+
class ${serviceName} {
|
|
108
|
+
public async index() {
|
|
109
|
+
return [];
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
public async show(id: number) {
|
|
113
|
+
return {};
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
public async create(body : Create${dtoName}) {
|
|
117
|
+
return {};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public async update(id: number, body: Update${dtoName}) {
|
|
121
|
+
return {};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public async remove(id: number) {
|
|
125
|
+
return {};
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export { ${serviceName} };
|
|
130
|
+
export default ${serviceName};
|
|
131
|
+
`);
|
|
132
|
+
fs_1.default.writeFileSync(dtoPath, `import {
|
|
133
|
+
IsString,
|
|
134
|
+
Min,
|
|
135
|
+
IsNotEmpty,
|
|
136
|
+
IsNumber,
|
|
137
|
+
} from "class-validator";
|
|
138
|
+
|
|
139
|
+
export class Create${dtoName} {
|
|
140
|
+
@IsString()
|
|
141
|
+
@IsNotEmpty()
|
|
142
|
+
name!: string;
|
|
143
|
+
|
|
144
|
+
@IsNumber()
|
|
145
|
+
@Min(0.1)
|
|
146
|
+
age!: number;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export class Update${dtoName} {
|
|
150
|
+
@IsString()
|
|
151
|
+
@IsNotEmpty()
|
|
152
|
+
name!: string;
|
|
153
|
+
|
|
154
|
+
@IsNumber()
|
|
155
|
+
@Min(0.1)
|
|
156
|
+
age!: number;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
`);
|
|
160
|
+
console.log(`
|
|
161
|
+
CREATE ${modulePath}
|
|
162
|
+
CREATE ${controllerPath}
|
|
163
|
+
CREATE ${servicePath}
|
|
164
|
+
CREATE ${dtoPath}
|
|
165
|
+
|
|
166
|
+
✔ Successfully generated module "${name}"
|
|
167
|
+
|
|
168
|
+
Controller ${controllerName}
|
|
169
|
+
Service ${serviceName}
|
|
170
|
+
DTO ${dtoName}
|
|
171
|
+
`);
|
|
172
|
+
}
|
|
173
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/cli/generators/module/index.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createService(root: string, name?: string): void;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createService = createService;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const shared_1 = require("../shared");
|
|
10
|
+
function createService(root, name) {
|
|
11
|
+
if (!name) {
|
|
12
|
+
console.log("Missing service path, try spear g service dog");
|
|
13
|
+
process.exit(1);
|
|
14
|
+
}
|
|
15
|
+
const resolvedPath = path_1.default.resolve(process.cwd(), root, 'modules', (0, shared_1.toPlural)(name));
|
|
16
|
+
const fileName = `${(0, shared_1.toSingular)(name)}.service.ts`;
|
|
17
|
+
const target = path_1.default.join(resolvedPath, fileName);
|
|
18
|
+
fs_1.default.mkdirSync(resolvedPath, { recursive: true });
|
|
19
|
+
const className = (0, shared_1.capitalize)((0, shared_1.toSingular)(name)) + "Service";
|
|
20
|
+
fs_1.default.writeFileSync(target, `
|
|
21
|
+
class ${className} {
|
|
22
|
+
public async index() {
|
|
23
|
+
return [];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
public async show(id: number) {
|
|
27
|
+
return {};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
public async create() {
|
|
31
|
+
return {};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public async update(id: number) {
|
|
35
|
+
return {};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public async remove(id: number) {
|
|
39
|
+
return {};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { ${className} };
|
|
44
|
+
export default ${className};
|
|
45
|
+
`);
|
|
46
|
+
console.log(`
|
|
47
|
+
CREATE ${target}
|
|
48
|
+
|
|
49
|
+
✔ Successfully generated service "${name}"
|
|
50
|
+
|
|
51
|
+
`);
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/cli/generators/service/index.ts"],"names":[],"mappings":";;;;;AAQA,sCAoDC;AA5DD,4CAAoB;AACpB,gDAAwB;AACxB,sCAImB;AAEnB,SAAgB,aAAa,CAAC,IAAa,EAAC,IAAa;IACvD,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO,CAAC,GAAG,CAAC,+CAA+C,CAAC,CAAC;QAC7D,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;IAED,MAAM,YAAY,GAAG,cAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,IAAI,EAAG,SAAS,EAAE,IAAA,iBAAQ,EAAC,IAAI,CAAC,CAAC,CAAC;IAEnF,MAAM,QAAQ,GAAG,GAAG,IAAA,mBAAU,EAAC,IAAI,CAAC,aAAa,CAAC;IAElD,MAAM,MAAM,GAAG,cAAI,CAAC,IAAI,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;IAEjD,YAAE,CAAC,SAAS,CAAC,YAAY,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAEhD,MAAM,SAAS,GAAG,IAAA,mBAAU,EAAC,IAAA,mBAAU,EAAC,IAAI,CAAC,CAAC,GAAG,SAAS,CAAC;IAE3D,YAAE,CAAC,aAAa,CACd,MAAM,EACN;QACI,SAAS;;;;;;;;;;;;;;;;;;;;;;WAsBN,SAAS;iBACH,SAAS;CACzB,CACE,CAAC;IAEH,OAAO,CAAC,GAAG,CAAC;WACF,MAAM;;oCAEmB,IAAI;;CAEvC,CAAC,CAAA;AACF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const ServiceTemplate = "\ntype Cat = {\n id: number;\n name: string;\n age: number;\n}\n\nexport class CatService {\n private cats: Cat[] = [\n { id: 1, name: 'cat1', age: 1.6 },\n { id: 2, name: 'cat2', age: 1.8 },\n ];\n public async index() {\n return this.cats;\n };\n\n public async show(id: number) {\n return this.cats.find(cat => cat.id === id);\n };\n\n public async create({ name, age }: Omit<Cat, \"id\">) {\n\n const cat: Cat = {\n id: this.cats.length + 1,\n name: name,\n age: age\n };\n\n this.cats.push(cat);\n\n return {\n message: \"Created\",\n cat\n };\n }\n \n public async update(id: number, { name, age }: Omit<Cat, \"id\">) {\n const index = this.cats.findIndex(d => d.id === id);\n\n if (index === -1) {\n throw new Error(\"Cat not found\");\n }\n\n this.cats[index] = {\n ...this.cats[index],\n ...{ name, age } \n };\n\n return {\n message: \"Updated\",\n cat: this.cats[index]\n };\n }\n\n public async remove(id: number) {\n const index = this.cats.findIndex(d => d.id === id);\n\n if (index === -1) {\n throw new Error(\"Cat not found\");\n }\n\n const [removedCat] = this.cats.splice(index, 1);\n\n return {\n message: \"Deleted\",\n cat: removedCat\n };\n }\n}\n";
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ServiceTemplate = void 0;
|
|
4
|
+
exports.ServiceTemplate = `
|
|
5
|
+
type Cat = {
|
|
6
|
+
id: number;
|
|
7
|
+
name: string;
|
|
8
|
+
age: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class CatService {
|
|
12
|
+
private cats: Cat[] = [
|
|
13
|
+
{ id: 1, name: 'cat1', age: 1.6 },
|
|
14
|
+
{ id: 2, name: 'cat2', age: 1.8 },
|
|
15
|
+
];
|
|
16
|
+
public async index() {
|
|
17
|
+
return this.cats;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
public async show(id: number) {
|
|
21
|
+
return this.cats.find(cat => cat.id === id);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
public async create({ name, age }: Omit<Cat, "id">) {
|
|
25
|
+
|
|
26
|
+
const cat: Cat = {
|
|
27
|
+
id: this.cats.length + 1,
|
|
28
|
+
name: name,
|
|
29
|
+
age: age
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
this.cats.push(cat);
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
message: "Created",
|
|
36
|
+
cat
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public async update(id: number, { name, age }: Omit<Cat, "id">) {
|
|
41
|
+
const index = this.cats.findIndex(d => d.id === id);
|
|
42
|
+
|
|
43
|
+
if (index === -1) {
|
|
44
|
+
throw new Error("Cat not found");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
this.cats[index] = {
|
|
48
|
+
...this.cats[index],
|
|
49
|
+
...{ name, age }
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
message: "Updated",
|
|
54
|
+
cat: this.cats[index]
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public async remove(id: number) {
|
|
59
|
+
const index = this.cats.findIndex(d => d.id === id);
|
|
60
|
+
|
|
61
|
+
if (index === -1) {
|
|
62
|
+
throw new Error("Cat not found");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const [removedCat] = this.cats.splice(index, 1);
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
message: "Deleted",
|
|
69
|
+
cat: removedCat
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
`;
|
|
74
|
+
//# sourceMappingURL=template.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"template.js","sourceRoot":"","sources":["../../../../src/cli/generators/service/template.ts"],"names":[],"mappings":";;;AACa,QAAA,eAAe,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAqE9B,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toSingular = toSingular;
|
|
4
|
+
exports.toPlural = toPlural;
|
|
5
|
+
exports.capitalize = capitalize;
|
|
6
|
+
function toSingular(name) {
|
|
7
|
+
if (!name)
|
|
8
|
+
return name;
|
|
9
|
+
return name.endsWith("s") ? name.slice(0, -1) : name;
|
|
10
|
+
}
|
|
11
|
+
function toPlural(name) {
|
|
12
|
+
if (!name)
|
|
13
|
+
return name;
|
|
14
|
+
if (name.endsWith("s"))
|
|
15
|
+
return name;
|
|
16
|
+
return name + "s";
|
|
17
|
+
}
|
|
18
|
+
function capitalize(value) {
|
|
19
|
+
return (value.charAt(0).toUpperCase() +
|
|
20
|
+
value.slice(1));
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/cli/generators/shared/index.ts"],"names":[],"mappings":";;AAAA,gCAGC;AAED,4BAMC;AAED,gCAOC;AApBD,SAAgB,UAAU,CAAC,IAAY;IACrC,IAAI,CAAC,IAAI;QAAE,OAAO,IAAI,CAAC;IACvB,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AACvD,CAAC;AAED,SAAgB,QAAQ,CAAC,IAAY;IACnC,IAAI,CAAC,IAAI;QAAE,OAAO,IAAI,CAAC;IAEvB,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,OAAO,IAAI,CAAC;IAEpC,OAAO,IAAI,GAAG,GAAG,CAAC;AACpB,CAAC;AAED,SAAgB,UAAU,CACxB,KAAa;IAEb,OAAO,CACL,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE;QAC7B,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CACf,CAAC;AACJ,CAAC"}
|
package/dist/cli/index.js
CHANGED
|
@@ -1,138 +1,65 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
-
};
|
|
6
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
`);
|
|
26
|
-
}
|
|
27
|
-
function createApp(targetPath) {
|
|
28
|
-
if (!targetPath) {
|
|
29
|
-
console.log("Missing target path");
|
|
30
|
-
process.exit(1);
|
|
31
|
-
}
|
|
32
|
-
const root = path_1.default.resolve(process.cwd(), targetPath);
|
|
33
|
-
fs_1.default.mkdirSync(root, {
|
|
34
|
-
recursive: true
|
|
35
|
-
});
|
|
36
|
-
fs_1.default.mkdirSync(path_1.default.join(root, "controllers"), {
|
|
37
|
-
recursive: true
|
|
38
|
-
});
|
|
39
|
-
fs_1.default.writeFileSync(path_1.default.join(root, "index.ts"), app_1.app);
|
|
40
|
-
fs_1.default.writeFileSync(path_1.default.join(root, "controllers", "cat.controller.ts"), controller_1.CatController);
|
|
41
|
-
console.log(`App created at: ${root}`);
|
|
4
|
+
const app_1 = require("./generators/app");
|
|
5
|
+
const module_1 = require("./generators/module");
|
|
6
|
+
const controller_1 = require("./generators/controller");
|
|
7
|
+
const service_1 = require("./generators/service");
|
|
8
|
+
const middleware_1 = require("./generators/middleware");
|
|
9
|
+
const dto_1 = require("./generators/dto");
|
|
10
|
+
const [, , command, type, name] = process.argv;
|
|
11
|
+
/**
|
|
12
|
+
* spear create new <name>
|
|
13
|
+
* spear g module <name>
|
|
14
|
+
* spear g controller <name>
|
|
15
|
+
* spear g service <name>
|
|
16
|
+
* spear g middleware <name>
|
|
17
|
+
*/
|
|
18
|
+
if (command === "create" && type === "new") {
|
|
19
|
+
(0, app_1.createApp)(name)
|
|
20
|
+
.then(_ => process.exit(1))
|
|
21
|
+
.catch(_ => process.exit(1));
|
|
42
22
|
}
|
|
43
|
-
|
|
23
|
+
if (command === "g") {
|
|
44
24
|
if (!name) {
|
|
45
|
-
|
|
25
|
+
printUsage();
|
|
46
26
|
process.exit(1);
|
|
47
27
|
}
|
|
48
|
-
const
|
|
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
|
-
@Get("/:id")
|
|
74
|
-
async show({
|
|
75
|
-
params
|
|
76
|
-
}: T.Context<{
|
|
77
|
-
params: {
|
|
78
|
-
id: number;
|
|
79
|
-
};
|
|
80
|
-
}>) {
|
|
81
|
-
|
|
82
|
-
return {
|
|
83
|
-
id: params.id
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
@Post("/")
|
|
88
|
-
async create({
|
|
89
|
-
body
|
|
90
|
-
}: T.Context<{
|
|
91
|
-
body: {};
|
|
92
|
-
}>) {
|
|
93
|
-
|
|
94
|
-
return {
|
|
95
|
-
body
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
@Put("/:id")
|
|
100
|
-
async update({
|
|
101
|
-
params,
|
|
102
|
-
body
|
|
103
|
-
}: T.Context<{
|
|
104
|
-
params: {
|
|
105
|
-
id: number;
|
|
106
|
-
};
|
|
107
|
-
body: {};
|
|
108
|
-
}>) {
|
|
109
|
-
|
|
110
|
-
return {
|
|
111
|
-
id: params.id,
|
|
112
|
-
body
|
|
113
|
-
};
|
|
114
|
-
}
|
|
28
|
+
const root = 'src';
|
|
29
|
+
switch (type) {
|
|
30
|
+
case "module":
|
|
31
|
+
(0, module_1.createModule)(root, name);
|
|
32
|
+
break;
|
|
33
|
+
case "controller":
|
|
34
|
+
(0, controller_1.createController)(root, name);
|
|
35
|
+
break;
|
|
36
|
+
case "service":
|
|
37
|
+
(0, service_1.createService)(root, name);
|
|
38
|
+
break;
|
|
39
|
+
case "dto":
|
|
40
|
+
(0, dto_1.createDto)(root, name);
|
|
41
|
+
break;
|
|
42
|
+
case "middleware":
|
|
43
|
+
(0, middleware_1.createMiddleware)(root, name);
|
|
44
|
+
break;
|
|
45
|
+
default:
|
|
46
|
+
printUsage();
|
|
47
|
+
}
|
|
48
|
+
process.exit(0);
|
|
49
|
+
}
|
|
50
|
+
function printUsage() {
|
|
51
|
+
console.log(`
|
|
52
|
+
Usage:
|
|
115
53
|
|
|
116
|
-
|
|
117
|
-
async remove({
|
|
118
|
-
params
|
|
119
|
-
}: T.Context<{
|
|
120
|
-
params: {
|
|
121
|
-
id: number;
|
|
122
|
-
};
|
|
123
|
-
}>) {
|
|
54
|
+
spear create new <project>
|
|
124
55
|
|
|
125
|
-
|
|
126
|
-
id: params.id
|
|
127
|
-
};
|
|
128
|
-
}
|
|
56
|
+
Generators:
|
|
129
57
|
|
|
130
|
-
|
|
58
|
+
spear g module <names>
|
|
59
|
+
spear g controller <name>
|
|
60
|
+
spear g service <name>
|
|
61
|
+
spear g dto <name>
|
|
62
|
+
spear g middleware <name>
|
|
131
63
|
`);
|
|
132
|
-
console.log(`Controller created: ${fileName}`);
|
|
133
|
-
}
|
|
134
|
-
function capitalize(value) {
|
|
135
|
-
return (value.charAt(0).toUpperCase() +
|
|
136
|
-
value.slice(1));
|
|
137
64
|
}
|
|
138
65
|
//# sourceMappingURL=index.js.map
|
package/dist/cli/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cli/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cli/index.ts"],"names":[],"mappings":";;;AAEA,0CAA6C;AAC7C,gDAAmD;AACnD,wDAA2D;AAC3D,kDAAqD;AACrD,wDAA2D;AAC3D,0CAA6C;AAE7C,MAAM,CAAC,EAAE,AAAD,EAAG,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;AAE/C;;;;;;GAMG;AAEH,IAAI,OAAO,KAAK,QAAQ,IAAI,IAAI,KAAK,KAAK,EAAE,CAAC;IAC3C,IAAA,eAAS,EAAC,IAAI,CAAC;SACd,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;SAC1B,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;AAC9B,CAAC;AAED,IAAI,OAAO,KAAK,GAAG,EAAE,CAAC;IACpB,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,UAAU,EAAE,CAAC;QACb,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;IAED,MAAM,IAAI,GAAG,KAAK,CAAC;IAEnB,QAAQ,IAAI,EAAE,CAAC;QAEb,KAAK,QAAQ;YACX,IAAA,qBAAY,EAAC,IAAI,EAAC,IAAI,CAAC,CAAC;YACxB,MAAM;QAER,KAAK,YAAY;YACf,IAAA,6BAAgB,EAAC,IAAI,EAAC,IAAI,CAAC,CAAC;YAC5B,MAAM;QAER,KAAK,SAAS;YACZ,IAAA,uBAAa,EAAC,IAAI,EAAC,IAAI,CAAC,CAAC;YACzB,MAAM;QAER,KAAK,KAAK;YACR,IAAA,eAAS,EAAC,IAAI,EAAC,IAAI,CAAC,CAAC;YACrB,MAAM;QAER,KAAK,YAAY;YACf,IAAA,6BAAgB,EAAC,IAAI,EAAC,IAAI,CAAC,CAAC;YAC5B,MAAM;QAER;YACE,UAAU,EAAE,CAAC;IACjB,CAAC;IAED,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC;AAED,SAAS,UAAU;IACjB,OAAO,CAAC,GAAG,CAAC;;;;;;;;;;;;CAYb,CAAC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const CatMiddleware = "\nimport { type T } from \"tspace-spear\";\n\nconst sleep = async (ms : number) => new Promise(resolve => setTimeout(resolve, ms))\n\nexport const CatMiddleware = async (ctx : T.Context, next: T.NextFunction) =>{\n await sleep(100);\n console.log('after logging middleware Meow!');\n ctx.req.cat = 'Meow!';\n return next()\n}\n";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CatMiddleware = void 0;
|
|
4
|
+
exports.CatMiddleware = `
|
|
5
|
+
import { type T } from "tspace-spear";
|
|
6
|
+
|
|
7
|
+
const sleep = async (ms : number) => new Promise(resolve => setTimeout(resolve, ms))
|
|
8
|
+
|
|
9
|
+
export const CatMiddleware = async (ctx : T.Context, next: T.NextFunction) =>{
|
|
10
|
+
await sleep(100);
|
|
11
|
+
console.log('after logging middleware Meow!');
|
|
12
|
+
ctx.req.cat = 'Meow!';
|
|
13
|
+
return next()
|
|
14
|
+
}
|
|
15
|
+
`;
|
|
16
|
+
//# sourceMappingURL=middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../src/cli/middleware.ts"],"names":[],"mappings":";;;AAAa,QAAA,aAAa,GAAG;;;;;;;;;;;CAW5B,CAAA"}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const client_1 = require("tspace-spear/client");
|
|
4
|
+
const client = new client_1.ApiClient("http://localhost:8000/api");
|
|
5
|
+
async function main() {
|
|
6
|
+
const res = await client.get("/cats");
|
|
7
|
+
res.data.cats = 1; // Type error: Type 'number' is not assignable to type '{ id: number; name: string; age: number; }[]'
|
|
8
|
+
res.data.cats[0].name = 1; // Type error: Type 'number' is not assignable to type 'string'
|
|
9
|
+
res.data.cats[0].age = "1.6"; // Type error: Type 'string' is not assignable to type 'number'
|
|
10
|
+
console.log(res);
|
|
11
|
+
// res.ok -> boolean
|
|
12
|
+
// res.status -> number
|
|
13
|
+
// res.data -> { cats: [{ id: 1, name: 'cat1', age: 1.6 },{ id: 2, name: 'cat2', age: 1.8 }] }
|
|
14
|
+
await client.get("/catsq"); // Type error: Argument of type '"/catsq"' is not assignable to parameter of type '"/cats" | "/cats/:id" | ... 3 more
|
|
15
|
+
}
|
|
16
|
+
main();
|
|
17
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":";;AAGA,gDAAgD;AAEhD,MAAM,MAAM,GAAyB,IAAI,kBAAS,CAChD,2BAA2B,CAC5B,CAAC;AAEF,KAAK,UAAU,IAAI;IAEf,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACtC,GAAG,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAA,CAAC,qGAAqG;IACvH,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAA,CAAC,+DAA+D;IACzF,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,KAAK,CAAA,CAAC,+DAA+D;IAE5F,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAChB,oBAAoB;IACpB,uBAAuB;IACvB,8FAA8F;IAE9F,MAAM,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,qHAAqH;AAErJ,CAAC;AACD,IAAI,EAAE,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LogMiddleware = void 0;
|
|
4
|
+
const sleep = async (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
5
|
+
const LogMiddleware = async (ctx, next) => {
|
|
6
|
+
await sleep(100);
|
|
7
|
+
console.log('after logging middleware Meow!');
|
|
8
|
+
ctx.req.cat = 'Meow!';
|
|
9
|
+
return next();
|
|
10
|
+
};
|
|
11
|
+
exports.LogMiddleware = LogMiddleware;
|
|
12
|
+
//# sourceMappingURL=log.middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log.middleware.js","sourceRoot":"","sources":["../../../src/common/middlewares/log.middleware.ts"],"names":[],"mappings":";;;AAGA,MAAM,KAAK,GAAG,KAAK,EAAE,EAAW,EAAE,EAAE,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAA;AAE7E,MAAM,aAAa,GAAG,KAAK,EAAE,GAAe,EAAE,IAAoB,EAAE,EAAE;IACzE,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC;IACjB,OAAO,CAAC,GAAG,CAAC,gCAAgC,CAAC,CAAC;IAC9C,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,OAAO,CAAC;IACtB,OAAO,IAAI,EAAE,CAAA;AACjB,CAAC,CAAA;AALY,QAAA,aAAa,iBAKzB"}
|
package/dist/index.d.ts
ADDED