@point-hub/papi 0.0.1
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/LICENSE +21 -0
- package/README.md +3 -0
- package/lib/app.d.ts +2 -0
- package/lib/app.js +10 -0
- package/lib/console/commands/make-command/index.command.d.ts +5 -0
- package/lib/console/commands/make-command/index.command.js +44 -0
- package/lib/console/commands/make-middleware/index.command.d.ts +7 -0
- package/lib/console/commands/make-middleware/index.command.js +72 -0
- package/lib/console/helper.d.ts +1 -0
- package/lib/console/helper.js +2 -0
- package/lib/console/index.d.ts +13 -0
- package/lib/console/index.js +21 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/server.d.ts +14 -0
- package/lib/server.js +47 -0
- package/package.json +71 -0
- package/stub/command/index.command.ts +16 -0
- package/stub/command/index.spec.ts +10 -0
- package/stub/middleware/configurable.middleware.ts +8 -0
- package/stub/middleware/configurable.spec.ts +12 -0
- package/stub/middleware/new.middleware.ts +5 -0
- package/stub/middleware/new.spec.ts +12 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 PointHub
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
## Description
|
|
2
|
+
|
|
3
|
+
Papi stands for Pointhub API. I made this because setting the project is kinda the bottleneck for me to make the ideas simply come true within a very short time, so I made this for Pointhub meet minimum requirement to create Backend API more easily, along with some good practices that I have learned.
|
package/lib/app.d.ts
ADDED
package/lib/app.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { BaseCommand, Color } from "@point-hub/express-cli";
|
|
4
|
+
import { pascalCase, kebabCase, camelCase } from "@point-hub/express-utils";
|
|
5
|
+
import { stubDir } from "../../../console/helper.js";
|
|
6
|
+
export default class MakeCommand extends BaseCommand {
|
|
7
|
+
constructor() {
|
|
8
|
+
super({
|
|
9
|
+
name: "make:command",
|
|
10
|
+
description: "Make a new console command",
|
|
11
|
+
summary: "Make a new console command",
|
|
12
|
+
arguments: [
|
|
13
|
+
{
|
|
14
|
+
name: "name",
|
|
15
|
+
description: "Name of console command",
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
options: [],
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
async handle() {
|
|
22
|
+
// Check if command directory is already exists
|
|
23
|
+
if (fs.existsSync(`${process.cwd()}/src/console/commands/${kebabCase(this.args.name)}`)) {
|
|
24
|
+
console.error(Color.red("Command directory is exists"));
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
// Create directory
|
|
28
|
+
fs.mkdirSync(`${process.cwd()}/src/console/commands/${kebabCase(this.args.name)}`, { recursive: true });
|
|
29
|
+
// Copy command file
|
|
30
|
+
const stubCommand = fs
|
|
31
|
+
.readFileSync(path.resolve(stubDir, "./command/index.command.ts"))
|
|
32
|
+
.toString()
|
|
33
|
+
.replace("[name]", kebabCase(this.args.name))
|
|
34
|
+
.replace("NewCommand", `${pascalCase(this.args.name)}Command`);
|
|
35
|
+
fs.writeFileSync(`${process.cwd()}/src/console/commands/${kebabCase(this.args.name)}/index.command.ts`, stubCommand);
|
|
36
|
+
// Copy test file
|
|
37
|
+
const stubTest = fs
|
|
38
|
+
.readFileSync(path.resolve(stubDir, "./command/index.spec.ts"))
|
|
39
|
+
.toString()
|
|
40
|
+
.replace(/NewCommand/g, `${pascalCase(this.args.name)}Command`)
|
|
41
|
+
.replace(/newCommand/g, `${camelCase(this.args.name)}Command`);
|
|
42
|
+
fs.writeFileSync(`${process.cwd()}/src/console/commands/${kebabCase(this.args.name)}/index.spec.ts`, stubTest);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { BaseCommand, Color } from "@point-hub/express-cli";
|
|
4
|
+
import { pascalCase, kebabCase } from "@point-hub/express-utils";
|
|
5
|
+
import { stubDir } from "../../../console/helper.js";
|
|
6
|
+
export default class MakeMiddleware extends BaseCommand {
|
|
7
|
+
constructor() {
|
|
8
|
+
super({
|
|
9
|
+
name: "make:middleware",
|
|
10
|
+
description: "Make a new middleware",
|
|
11
|
+
summary: "Make a new middleware",
|
|
12
|
+
arguments: [
|
|
13
|
+
{
|
|
14
|
+
name: "name",
|
|
15
|
+
description: "Name of middleware",
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
options: [
|
|
19
|
+
{
|
|
20
|
+
type: "boolean",
|
|
21
|
+
flag: "--configurable",
|
|
22
|
+
shorthand: "-c",
|
|
23
|
+
description: "Export a function which accepts an options",
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async handle() {
|
|
29
|
+
// Check if middleware directory is already exists
|
|
30
|
+
if (fs.existsSync(`${process.cwd()}/src/middleware/${kebabCase(this.args.name)}`)) {
|
|
31
|
+
console.error(Color.red("Middleware directory is exists"));
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
// Create middleware directory
|
|
35
|
+
fs.mkdirSync(`${process.cwd()}/src/middleware/${kebabCase(this.args.name)}`, { recursive: true });
|
|
36
|
+
// Copy middleware
|
|
37
|
+
if (this.opts["--configurable"]) {
|
|
38
|
+
this.copyConfigureableMiddleware();
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
this.copyMiddleware();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
copyMiddleware() {
|
|
45
|
+
// Copy middleware file
|
|
46
|
+
const stubMiddleware = fs
|
|
47
|
+
.readFileSync(path.resolve(stubDir, "./middleware/new.middleware.ts"))
|
|
48
|
+
.toString()
|
|
49
|
+
.replace("NewMiddleware", `${pascalCase(this.args.name)}Middleware`);
|
|
50
|
+
fs.writeFileSync(`${process.cwd()}/src/middleware/${kebabCase(this.args.name)}/index.middleware.ts`, stubMiddleware);
|
|
51
|
+
// Copy test file
|
|
52
|
+
const stubTest = fs
|
|
53
|
+
.readFileSync(path.resolve(stubDir, "./middleware/new.spec.ts"))
|
|
54
|
+
.toString()
|
|
55
|
+
.replace("new.middleware.js", `index.middleware.js`);
|
|
56
|
+
fs.writeFileSync(`${process.cwd()}/src/middleware/${kebabCase(this.args.name)}/index.spec.ts`, stubTest);
|
|
57
|
+
}
|
|
58
|
+
copyConfigureableMiddleware() {
|
|
59
|
+
// Copy middleware file
|
|
60
|
+
const stubMiddleware = fs
|
|
61
|
+
.readFileSync(path.resolve(stubDir, "./middleware/configurable.middleware.ts"))
|
|
62
|
+
.toString()
|
|
63
|
+
.replace("NewMiddleware", `${pascalCase(this.args.name)}Middleware`);
|
|
64
|
+
fs.writeFileSync(`${process.cwd()}/src/middleware/${kebabCase(this.args.name)}/index.middleware.ts`, stubMiddleware);
|
|
65
|
+
// Copy test file
|
|
66
|
+
const stubTest = fs
|
|
67
|
+
.readFileSync(path.resolve(stubDir, "./middleware/configurable.spec.ts"))
|
|
68
|
+
.toString()
|
|
69
|
+
.replace("configurable.middleware.js", `index.middleware.js`);
|
|
70
|
+
fs.writeFileSync(`${process.cwd()}/src/middleware/${kebabCase(this.args.name)}/index.spec.ts`, stubTest);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const stubDir: string;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ExpressCli } from "@point-hub/express-cli";
|
|
2
|
+
export declare class ConsoleKernel {
|
|
3
|
+
path: string;
|
|
4
|
+
private command;
|
|
5
|
+
constructor(command: ExpressCli);
|
|
6
|
+
/**
|
|
7
|
+
* Register the commands for the application.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* command.register(new ExampleCommand());
|
|
11
|
+
*/
|
|
12
|
+
register(): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { URL } from "url";
|
|
2
|
+
import { fileSearch } from "@point-hub/express-utils";
|
|
3
|
+
export class ConsoleKernel {
|
|
4
|
+
constructor(command) {
|
|
5
|
+
this.path = new URL(".", import.meta.url).pathname;
|
|
6
|
+
this.command = command;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Register the commands for the application.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* command.register(new ExampleCommand());
|
|
13
|
+
*/
|
|
14
|
+
async register() {
|
|
15
|
+
const result = await fileSearch("/*.command.(js|ts)", this.path, { maxDeep: 2, regExp: true });
|
|
16
|
+
for (let i = 0; i < result.length; i++) {
|
|
17
|
+
const { default: Command } = await import(`./${result[i].path}`);
|
|
18
|
+
this.command.register(new Command());
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Server as BaseServer } from "./server.js";
|
package/lib/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Server as BaseServer } from "./server.js";
|
package/lib/server.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Server as HttpServer } from "http";
|
|
3
|
+
import { Express } from "express";
|
|
4
|
+
export declare class Server {
|
|
5
|
+
app: Express;
|
|
6
|
+
server: HttpServer | null;
|
|
7
|
+
constructor(app: Express);
|
|
8
|
+
listen(port: number, hostname?: string): Promise<unknown>;
|
|
9
|
+
start(port: number, hostname?: string): Promise<void>;
|
|
10
|
+
stop(): void;
|
|
11
|
+
get host(): string;
|
|
12
|
+
get port(): number;
|
|
13
|
+
get url(): string;
|
|
14
|
+
}
|
package/lib/server.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export class Server {
|
|
2
|
+
constructor(app) {
|
|
3
|
+
this.server = null;
|
|
4
|
+
this.app = app;
|
|
5
|
+
}
|
|
6
|
+
listen(port, hostname) {
|
|
7
|
+
return new Promise((resolve, reject) => {
|
|
8
|
+
if (hostname) {
|
|
9
|
+
this.server = this.app.listen(port, hostname).once("listening", resolve).once("error", reject);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
this.server = this.app.listen(port).once("listening", resolve).once("error", reject);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
async start(port, hostname) {
|
|
17
|
+
try {
|
|
18
|
+
await this.listen(port, hostname);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
stop() {
|
|
25
|
+
var _a;
|
|
26
|
+
(_a = this.server) === null || _a === void 0 ? void 0 : _a.close();
|
|
27
|
+
this.server = null;
|
|
28
|
+
}
|
|
29
|
+
get host() {
|
|
30
|
+
var _a;
|
|
31
|
+
const address = (_a = this.server) === null || _a === void 0 ? void 0 : _a.address();
|
|
32
|
+
if ((address === null || address === void 0 ? void 0 : address.address) === "0.0.0.0" || (address === null || address === void 0 ? void 0 : address.address) === "::") {
|
|
33
|
+
return "localhost";
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
return address === null || address === void 0 ? void 0 : address.address;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
get port() {
|
|
40
|
+
var _a;
|
|
41
|
+
const address = (_a = this.server) === null || _a === void 0 ? void 0 : _a.address();
|
|
42
|
+
return address === null || address === void 0 ? void 0 : address.port;
|
|
43
|
+
}
|
|
44
|
+
get url() {
|
|
45
|
+
return `http://${this.host}${this.port !== 80 ? `:${this.port}` : ""}`;
|
|
46
|
+
}
|
|
47
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@point-hub/papi",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Point API Framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib/",
|
|
9
|
+
"stub/"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"dev:compile": "tsc -w --project tsconfig.build.json",
|
|
13
|
+
"dev:resolve-path": "tsc-alias -w --project tsconfig.build.json",
|
|
14
|
+
"dev": "shx rm -rf lib && concurrently -k npm:dev:*",
|
|
15
|
+
"build:compile": "tsc --project tsconfig.build.json",
|
|
16
|
+
"build:resolve-path": "tsc-alias --project tsconfig.build.json",
|
|
17
|
+
"build": "shx rm -rf lib && npm run build:compile && npm run build:resolve-path",
|
|
18
|
+
"lint": "eslint ./src/**/*.ts",
|
|
19
|
+
"lint:fix": "npm run lint -- --fix",
|
|
20
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest --runInBand",
|
|
21
|
+
"test:coverage": "shx rm -rf coverage && npm run test -- --coverage"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"papi",
|
|
25
|
+
"express",
|
|
26
|
+
"api",
|
|
27
|
+
"boilerplate",
|
|
28
|
+
"framework"
|
|
29
|
+
],
|
|
30
|
+
"author": "Martien Dermawan Tanama <martiendt@gmail.com>",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/point-hub/papi.git"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@babel/core": "^7.18.6",
|
|
38
|
+
"@babel/preset-env": "^7.18.6",
|
|
39
|
+
"@babel/preset-typescript": "^7.18.6",
|
|
40
|
+
"@types/compression": "^1.7.2",
|
|
41
|
+
"@types/cors": "^2.8.12",
|
|
42
|
+
"@types/express": "^4.17.13",
|
|
43
|
+
"@types/jest": "^28.1.4",
|
|
44
|
+
"@types/node": "^18.0.3",
|
|
45
|
+
"@types/shelljs": "^0.8.11",
|
|
46
|
+
"@types/supertest": "^2.0.12",
|
|
47
|
+
"@typescript-eslint/eslint-plugin": "^5.25.0",
|
|
48
|
+
"babel-jest": "^28.1.2",
|
|
49
|
+
"concurrently": "^7.2.2",
|
|
50
|
+
"eslint": "^8.15.0",
|
|
51
|
+
"eslint-config-prettier": "^8.5.0",
|
|
52
|
+
"eslint-import-resolver-typescript": "^3.2.4",
|
|
53
|
+
"eslint-plugin-import": "^2.26.0",
|
|
54
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
55
|
+
"jest": "^28.1.2",
|
|
56
|
+
"prettier": "^2.7.1",
|
|
57
|
+
"shx": "^0.3.4",
|
|
58
|
+
"supertest": "^6.2.4",
|
|
59
|
+
"tsc-alias": "^1.6.11",
|
|
60
|
+
"typescript": "^4.7.4"
|
|
61
|
+
},
|
|
62
|
+
"dependencies": {
|
|
63
|
+
"@point-hub/express-cli": "^0.2.2",
|
|
64
|
+
"@point-hub/express-error-handler": "^0.0.1",
|
|
65
|
+
"@point-hub/express-utils": "^0.0.14",
|
|
66
|
+
"shelljs": "^0.8.5"
|
|
67
|
+
},
|
|
68
|
+
"peerDependencies": {
|
|
69
|
+
"express": "^4.18.1"
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { BaseCommand } from "@point-hub/express-cli";
|
|
2
|
+
|
|
3
|
+
export default class NewCommand extends BaseCommand {
|
|
4
|
+
constructor() {
|
|
5
|
+
super({
|
|
6
|
+
name: "[name]",
|
|
7
|
+
description: "[description]",
|
|
8
|
+
summary: "[summary]",
|
|
9
|
+
arguments: [],
|
|
10
|
+
options: [],
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
async handle(): Promise<void> {
|
|
14
|
+
//
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { jest } from "@jest/globals";
|
|
2
|
+
import NewCommand from "./index.command.js";
|
|
3
|
+
|
|
4
|
+
it("test command", () => {
|
|
5
|
+
const newCommand = new NewCommand();
|
|
6
|
+
const spy = jest.spyOn(newCommand, "handle");
|
|
7
|
+
newCommand.handle();
|
|
8
|
+
|
|
9
|
+
expect(spy).toBeCalled();
|
|
10
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jest } from "@jest/globals";
|
|
2
|
+
import { Request, Response, NextFunction } from "express";
|
|
3
|
+
import middleware from "./configurable.middleware.js";
|
|
4
|
+
|
|
5
|
+
it("test middleware", () => {
|
|
6
|
+
const req = {} as Request;
|
|
7
|
+
const res = {} as Response;
|
|
8
|
+
const next: NextFunction = jest.fn();
|
|
9
|
+
middleware()(req, res, next);
|
|
10
|
+
|
|
11
|
+
expect(next).toBeCalled();
|
|
12
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jest } from "@jest/globals";
|
|
2
|
+
import { Request, Response, NextFunction } from "express";
|
|
3
|
+
import middleware from "./new.middleware.js";
|
|
4
|
+
|
|
5
|
+
it("test middleware", () => {
|
|
6
|
+
const req = {} as Request;
|
|
7
|
+
const res = {} as Response;
|
|
8
|
+
const next: NextFunction = jest.fn();
|
|
9
|
+
middleware(req, res, next);
|
|
10
|
+
|
|
11
|
+
expect(next).toBeCalled();
|
|
12
|
+
});
|