alp-node 6.2.0 → 8.0.0

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.
Files changed (66) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +7 -7
  3. package/dist/AlpNodeApp-node20.mjs +367 -0
  4. package/dist/AlpNodeApp-node20.mjs.map +1 -0
  5. package/dist/definitions/AlpNodeApp.d.ts +21 -14
  6. package/dist/definitions/AlpNodeApp.d.ts.map +1 -1
  7. package/dist/definitions/config.d.ts +17 -0
  8. package/dist/definitions/config.d.ts.map +1 -0
  9. package/dist/definitions/errors.d.ts +3 -0
  10. package/dist/definitions/errors.d.ts.map +1 -0
  11. package/dist/definitions/index.d.ts +9 -7
  12. package/dist/definitions/index.d.ts.map +1 -1
  13. package/dist/definitions/language.d.ts +7 -0
  14. package/dist/definitions/language.d.ts.map +1 -0
  15. package/dist/definitions/listen.d.ts +6 -0
  16. package/dist/definitions/listen.d.ts.map +1 -0
  17. package/dist/definitions/params/ParamValid.d.ts +8 -0
  18. package/dist/definitions/params/ParamValid.d.ts.map +1 -0
  19. package/dist/definitions/params/ParamValidationResult.d.ts +9 -0
  20. package/dist/definitions/params/ParamValidationResult.d.ts.map +1 -0
  21. package/dist/definitions/params/ParamValidationResult.test.d.ts +2 -0
  22. package/dist/definitions/params/ParamValidationResult.test.d.ts.map +1 -0
  23. package/dist/definitions/params/ParamValueFromContext.d.ts +12 -0
  24. package/dist/definitions/params/ParamValueFromContext.d.ts.map +1 -0
  25. package/dist/definitions/params/ParamValueModelValidator.d.ts +4 -0
  26. package/dist/definitions/params/ParamValueModelValidator.d.ts.map +1 -0
  27. package/dist/definitions/params/ParamValueStringValidator.d.ts +5 -0
  28. package/dist/definitions/params/ParamValueStringValidator.d.ts.map +1 -0
  29. package/dist/definitions/params/ParamValueValidator.d.ts +10 -0
  30. package/dist/definitions/params/ParamValueValidator.d.ts.map +1 -0
  31. package/dist/definitions/params/index.d.ts +19 -0
  32. package/dist/definitions/params/index.d.ts.map +1 -0
  33. package/dist/definitions/router.d.ts +13 -0
  34. package/dist/definitions/router.d.ts.map +1 -0
  35. package/dist/definitions/translate/index.d.ts +11 -0
  36. package/dist/definitions/translate/index.d.ts.map +1 -0
  37. package/dist/definitions/translate/load.d.ts +4 -0
  38. package/dist/definitions/translate/load.d.ts.map +1 -0
  39. package/dist/definitions/types.d.ts +53 -0
  40. package/dist/definitions/types.d.ts.map +1 -0
  41. package/dist/index-node20.mjs +495 -0
  42. package/dist/index-node20.mjs.map +1 -0
  43. package/package.json +27 -28
  44. package/src/AlpNodeApp.ts +71 -50
  45. package/src/config.ts +116 -0
  46. package/src/errors.ts +70 -0
  47. package/src/index.ts +33 -18
  48. package/src/language.ts +35 -0
  49. package/src/listen.ts +68 -0
  50. package/src/params/ParamValid.ts +15 -0
  51. package/src/params/ParamValidationResult.test.ts +65 -0
  52. package/src/params/ParamValidationResult.ts +38 -0
  53. package/src/params/ParamValueFromContext.ts +42 -0
  54. package/src/params/ParamValueModelValidator.ts +36 -0
  55. package/src/params/ParamValueStringValidator.ts +13 -0
  56. package/src/params/ParamValueValidator.ts +23 -0
  57. package/src/params/index.ts +70 -0
  58. package/src/router.ts +64 -0
  59. package/src/translate/index.ts +45 -0
  60. package/src/translate/load.ts +31 -0
  61. package/src/types.ts +67 -0
  62. package/dist/AlpNodeApp-node18.mjs +0 -94
  63. package/dist/AlpNodeApp-node18.mjs.map +0 -1
  64. package/dist/index-node18.mjs +0 -124
  65. package/dist/index-node18.mjs.map +0 -1
  66. package/src/.eslintrc.json +0 -31
@@ -1,124 +0,0 @@
1
- import { readFileSync, existsSync } from 'node:fs';
2
- import path from 'node:path';
3
- import _config, { Config } from 'alp-node-config';
4
- export { Config } from 'alp-node-config';
5
- import { Logger } from 'nightingale-logger';
6
- import { deprecate } from 'node:util';
7
- import _listen from 'alp-listen';
8
- import errors from 'alp-node-errors';
9
- import language from 'alp-node-language';
10
- import params from 'alp-params';
11
- import translate from 'alp-translate';
12
- import Koa from 'koa';
13
- import compress from 'koa-compress';
14
- import serve from 'koa-static';
15
-
16
- const logger$1 = new Logger('alp');
17
- class AlpNodeApp extends Koa {
18
- /**
19
- * @param {Object} [options]
20
- * @param {string} [options.certPath] directory of the ssl certificates
21
- * @param {string} [options.publicPath] directory of public files
22
- */
23
- constructor({
24
- appDirname,
25
- packageDirname,
26
- config,
27
- certPath,
28
- publicPath
29
- }) {
30
- super();
31
- this.dirname = path.normalize(appDirname);
32
- Object.defineProperty(this, 'packageDirname', {
33
- get: deprecate(() => packageDirname, 'packageDirname'),
34
- configurable: false,
35
- enumerable: false
36
- });
37
- this.certPath = certPath || `${packageDirname}/config/cert`;
38
- this.publicPath = publicPath || `${packageDirname}/public/`;
39
- this.config = _config(this, config);
40
- this.context.config = this.config;
41
- params(this);
42
- language(this);
43
- translate('locales')(this);
44
- this.use(compress());
45
- }
46
- existsConfigSync(name) {
47
- return this.config.existsConfigSync(name);
48
- }
49
- loadConfigSync(name) {
50
- return this.config.loadConfigSync(name);
51
- }
52
- createContext(req, res) {
53
- const ctx = super.createContext(req, res);
54
- ctx.sanitizedState = {};
55
- return ctx;
56
- }
57
- servePublic() {
58
- this.use(serve(this.publicPath)); // static files
59
- }
60
- catchErrors() {
61
- this.use(errors);
62
- }
63
-
64
- // eslint-disable-next-line @typescript-eslint/class-methods-use-this
65
- listen() {
66
- throw new Error('Use start instead');
67
- }
68
-
69
- /**
70
- * Close server and emit close event
71
- */
72
- close() {
73
- if (this._server) {
74
- this._server.close();
75
- this.emit('close');
76
- }
77
- }
78
- async start(fn) {
79
- await fn();
80
- try {
81
- const server = await _listen(this.config, this.callback(), this.certPath);
82
- this._server = server;
83
- logger$1.success('started');
84
- if (process.send) process.send('ready');
85
- return server;
86
- } catch (error) {
87
- logger$1.error('start fail', {
88
- err: error
89
- });
90
- throw error;
91
- }
92
- }
93
- }
94
-
95
- const logger = new Logger('alp');
96
- const appDirname = path.resolve('build');
97
- const packagePath = path.resolve('package.json');
98
- if (!packagePath) {
99
- throw new Error(`Could not find package.json: "${String(packagePath)}"`);
100
- }
101
- const packageDirname = path.dirname(packagePath);
102
- logger.debug('init', {
103
- appDirname,
104
- packageDirname
105
- });
106
- const packageConfig = JSON.parse(readFileSync(packagePath, 'utf8'));
107
- const buildedConfigPath = `${appDirname}/build/config/`;
108
- const configPath = existsSync(buildedConfigPath) ? buildedConfigPath : `${appDirname}/config/`;
109
- const config = new Config(configPath).loadSync({
110
- packageConfig
111
- });
112
- class App extends AlpNodeApp {
113
- constructor(options) {
114
- super({
115
- ...options,
116
- appDirname,
117
- packageDirname,
118
- config
119
- });
120
- }
121
- }
122
-
123
- export { appDirname, config, App as default, packageConfig, packageDirname };
124
- //# sourceMappingURL=index-node18.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index-node18.mjs","sources":["../src/AlpNodeApp.ts","../src/index.ts"],"sourcesContent":["import type { IncomingMessage, Server, ServerResponse } from 'node:http';\nimport path from 'node:path';\nimport { deprecate } from 'node:util';\nimport _listen from 'alp-listen';\nimport type { Config } from 'alp-node-config';\nimport _config from 'alp-node-config';\nimport errors from 'alp-node-errors';\nimport language from 'alp-node-language';\nimport params from 'alp-params';\nimport translate from 'alp-translate';\nimport type {\n NodeApplication,\n NodeConfig,\n Context as AlpContext,\n ContextState,\n ContextSanitizedState,\n ContextRequest,\n} from 'alp-types';\nimport Koa from 'koa';\nimport type { ParameterizedContext, DefaultState, BaseRequest } from 'koa';\nimport compress from 'koa-compress';\nimport serve from 'koa-static';\nimport { Logger } from 'nightingale-logger';\n\nconst logger = new Logger('alp');\n\nexport interface AlpNodeAppOptions {\n appDirname: string;\n packageDirname: string;\n config: Config & NodeConfig;\n certPath?: string;\n publicPath?: string;\n}\n\ndeclare module 'koa' {\n // eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/no-shadow\n interface DefaultState extends ContextState {}\n // eslint-disable-next-line @typescript-eslint/no-empty-interface\n interface DefaultContext extends AlpContext {}\n // eslint-disable-next-line @typescript-eslint/no-empty-interface\n interface BaseContext extends AlpContext {}\n // eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/no-shadow\n interface BaseRequest extends ContextRequest {}\n}\n\nexport class AlpNodeApp extends Koa<ContextState> implements NodeApplication {\n dirname: string;\n\n certPath: string;\n\n publicPath: string;\n\n config: Config & NodeConfig;\n\n declare request: BaseRequest & ContextRequest;\n\n _server?: Server;\n\n /**\n * @param {Object} [options]\n * @param {string} [options.certPath] directory of the ssl certificates\n * @param {string} [options.publicPath] directory of public files\n */\n constructor({\n appDirname,\n packageDirname,\n config,\n certPath,\n publicPath,\n }: AlpNodeAppOptions) {\n super();\n\n this.dirname = path.normalize(appDirname);\n\n Object.defineProperty(this, 'packageDirname', {\n get: deprecate(() => packageDirname, 'packageDirname'),\n configurable: false,\n enumerable: false,\n });\n\n this.certPath = certPath || `${packageDirname}/config/cert`;\n this.publicPath = publicPath || `${packageDirname}/public/`;\n\n this.config = _config(this, config);\n this.context.config = this.config;\n\n params(this);\n language(this);\n translate('locales')(this);\n\n this.use(compress());\n }\n\n existsConfigSync(name: string): ReturnType<Config['existsConfigSync']> {\n return this.config.existsConfigSync(name);\n }\n\n loadConfigSync(name: string): ReturnType<Config['loadConfigSync']> {\n return this.config.loadConfigSync(name);\n }\n\n override createContext<StateT = DefaultState>(\n req: IncomingMessage,\n res: ServerResponse,\n ): ParameterizedContext<StateT> {\n const ctx = super.createContext<StateT>(req, res);\n ctx.sanitizedState = {} as ContextSanitizedState;\n return ctx;\n }\n\n servePublic(): void {\n this.use(serve(this.publicPath)); // static files\n }\n\n catchErrors(): void {\n this.use(errors);\n }\n\n // eslint-disable-next-line @typescript-eslint/class-methods-use-this\n override listen(): never {\n throw new Error('Use start instead');\n }\n\n /**\n * Close server and emit close event\n */\n close(): void {\n if (this._server) {\n this._server.close();\n this.emit('close');\n }\n }\n\n async start(fn: () => Promise<void> | void): Promise<Server> {\n await fn();\n try {\n const server = await _listen(this.config, this.callback(), this.certPath);\n this._server = server;\n logger.success('started');\n if (process.send) process.send('ready');\n return server;\n } catch (error: unknown) {\n logger.error('start fail', { err: error });\n throw error;\n }\n }\n}\n\nexport type { Context } from 'koa';\n","import { existsSync, readFileSync } from 'node:fs';\nimport path from 'node:path';\nimport { Config } from 'alp-node-config';\nimport { Logger } from 'nightingale-logger';\nimport type { AlpNodeAppOptions } from './AlpNodeApp';\nimport { AlpNodeApp } from './AlpNodeApp';\n\nexport type { Context } from './AlpNodeApp';\nexport { Config } from 'alp-node-config';\n\nconst logger = new Logger('alp');\n\nexport const appDirname = path.resolve('build');\n\nconst packagePath = path.resolve('package.json');\nif (!packagePath) {\n throw new Error(`Could not find package.json: \"${String(packagePath)}\"`);\n}\nexport const packageDirname = path.dirname(packagePath);\n\nlogger.debug('init', { appDirname, packageDirname });\n\nexport const packageConfig: Record<string, unknown> = JSON.parse(\n readFileSync(packagePath, 'utf8'),\n) as Record<string, unknown>;\n\nconst buildedConfigPath = `${appDirname}/build/config/`;\nconst configPath = existsSync(buildedConfigPath)\n ? buildedConfigPath\n : `${appDirname}/config/`;\n\nexport const config = new Config(configPath).loadSync({ packageConfig });\n\nexport type AppOptions = Omit<\n AlpNodeAppOptions,\n 'appDirname' | 'config' | 'packageDirname'\n>;\n\nexport default class App extends AlpNodeApp {\n constructor(options?: AppOptions) {\n super({\n ...options,\n appDirname,\n packageDirname,\n config,\n });\n }\n}\n"],"names":["logger","Logger","AlpNodeApp","Koa","constructor","appDirname","packageDirname","config","certPath","publicPath","dirname","path","normalize","Object","defineProperty","get","deprecate","configurable","enumerable","_config","context","params","language","translate","use","compress","existsConfigSync","name","loadConfigSync","createContext","req","res","ctx","sanitizedState","servePublic","serve","catchErrors","errors","listen","Error","close","_server","emit","start","fn","server","_listen","callback","success","process","send","error","err","resolve","packagePath","String","debug","packageConfig","JSON","parse","readFileSync","buildedConfigPath","configPath","existsSync","Config","loadSync","App","options"],"mappings":";;;;;;;;;;;;;;;AAwBA,MAAMA,Q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AC;AAOzD,MAAMS,GAAG,SAAShE,UAAU,CAAC;EAC1CE,WAAWA,CAAC+D,OAAoB,EAAE;AAChC,IAAA,KAAK,CAAC;AACJ,MAAA,GAAGA,OAAO;MACV9D,UAAU;MACVC,cAAc;AACdC,MAAAA,MAAAA;AACF,KAAC,CAAC,CAAA;AACJ,GAAA;AACF;;;;"}
@@ -1,31 +0,0 @@
1
- {
2
- "root": true,
3
- "parser": "@typescript-eslint/parser",
4
- "parserOptions": {
5
- "EXPERIMENTAL_useProjectService": true,
6
- "project": "packages/alp-node/tsconfig.json"
7
- },
8
- "plugins": ["@typescript-eslint"],
9
- "extends": [
10
- "@pob/eslint-config-typescript",
11
- "@pob/eslint-config-typescript/node"
12
- ],
13
- "ignorePatterns": ["*.d.ts"],
14
- "overrides": [
15
- {
16
- "files": ["**/*.test.ts", "__tests__/**/*.ts"],
17
- "extends": ["@pob/eslint-config-typescript/test"],
18
- "env": {
19
- "jest": true
20
- },
21
- "rules": {
22
- "import/no-extraneous-dependencies": [
23
- "error",
24
- {
25
- "devDependencies": true
26
- }
27
- ]
28
- }
29
- }
30
- ]
31
- }