@where-org/where-common 2.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 +1 -0
- package/index.d.ts +5 -0
- package/index.js +1 -0
- package/lib/common/config/config.js +147 -0
- package/lib/common/config/index.js +1 -0
- package/lib/common/file/index.js +4 -0
- package/lib/common/file/read.js +46 -0
- package/lib/common/index-browser.js +4 -0
- package/lib/common/index.js +7 -0
- package/lib/common/init/credential.js +65 -0
- package/lib/common/init/emitter.js +13 -0
- package/lib/common/init/index-browser.js +8 -0
- package/lib/common/init/index.js +9 -0
- package/lib/common/init/log.js +14 -0
- package/lib/common/spec/at-spec.yaml +105 -0
- package/lib/common/spec/index.js +1 -0
- package/lib/common/spec/spec.js +206 -0
- package/lib/common/util/casing.js +90 -0
- package/lib/common/util/cast.js +18 -0
- package/lib/common/util/date.js +25 -0
- package/lib/common/util/index.js +7 -0
- package/lib/common/util/url.js +50 -0
- package/lib/common.js +6 -0
- package/lib/cq.js +122 -0
- package/lib/da.js +291 -0
- package/lib/define.js +22 -0
- package/lib/exception.js +109 -0
- package/lib/types/common.d.ts +188 -0
- package/lib/types/cq.d.ts +41 -0
- package/lib/types/da.d.ts +32 -0
- package/lib/types/define.d.ts +11 -0
- package/lib/types/exception.d.ts +19 -0
- package/package.json +22 -0
package/lib/exception.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { status } from './define.js';
|
|
2
|
+
|
|
3
|
+
class ConnectionException extends Error {
|
|
4
|
+
|
|
5
|
+
constructor(message, ...options) {
|
|
6
|
+
|
|
7
|
+
super(message, ...options);
|
|
8
|
+
|
|
9
|
+
Object.defineProperty(this, 'name', {
|
|
10
|
+
value: this.constructor.name,
|
|
11
|
+
configurable: true,
|
|
12
|
+
enumerable: false,
|
|
13
|
+
writable: true,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
if (Error.captureStackTrace) {
|
|
17
|
+
Error.captureStackTrace(this, ConnectionException);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
class UrlException extends Error {
|
|
25
|
+
|
|
26
|
+
constructor(message, ...options) {
|
|
27
|
+
|
|
28
|
+
super(message, ...options);
|
|
29
|
+
|
|
30
|
+
Object.defineProperty(this, 'name', {
|
|
31
|
+
value: this.constructor.name,
|
|
32
|
+
configurable: true,
|
|
33
|
+
enumerable: false,
|
|
34
|
+
writable: true,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
if (Error.captureStackTrace) {
|
|
38
|
+
Error.captureStackTrace(this, UrlException);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
class ServerException extends Error {
|
|
46
|
+
|
|
47
|
+
static header = 'X-Where-Server-Exception';
|
|
48
|
+
|
|
49
|
+
constructor(message, debug = null, ...options) {
|
|
50
|
+
|
|
51
|
+
const code = Object.values(status).reduce((o, v) => ({ [v.number]: v, ...o }), {})[message];
|
|
52
|
+
|
|
53
|
+
if (!code) {
|
|
54
|
+
throw new Error('Invalid "ServerException" argument');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
super(code.message, ...options);
|
|
58
|
+
|
|
59
|
+
this.status = { code };
|
|
60
|
+
this.debug = debug;
|
|
61
|
+
|
|
62
|
+
Object.defineProperty(this, 'name', {
|
|
63
|
+
value: this.constructor.name,
|
|
64
|
+
configurable: true,
|
|
65
|
+
enumerable: false,
|
|
66
|
+
writable: true,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
if (Error.captureStackTrace) {
|
|
70
|
+
Error.captureStackTrace(this, ServerException);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
class ServerError extends Error {
|
|
78
|
+
|
|
79
|
+
static header = 'X-Where-Server-Error';
|
|
80
|
+
|
|
81
|
+
constructor(message, debug = null, ...options) {
|
|
82
|
+
|
|
83
|
+
const code = Object.values(status).reduce((o, v) => ({ [v.number]: v, ...o }), {})[message];
|
|
84
|
+
|
|
85
|
+
if (!code) {
|
|
86
|
+
throw new Error('Invalid "ServerException" argument');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
super(code.message, ...options);
|
|
90
|
+
|
|
91
|
+
this.status = { code };
|
|
92
|
+
this.debug = debug;
|
|
93
|
+
|
|
94
|
+
Object.defineProperty(this, 'name', {
|
|
95
|
+
value: this.constructor.name,
|
|
96
|
+
configurable: true,
|
|
97
|
+
enumerable: false,
|
|
98
|
+
writable: true,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
if (Error.captureStackTrace) {
|
|
102
|
+
Error.captureStackTrace(this, ServerError);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export { ConnectionException, UrlException, ServerException, ServerError };
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import type { DataObject } from './da.js';
|
|
2
|
+
|
|
3
|
+
// lib/common/file/config.js
|
|
4
|
+
|
|
5
|
+
type Mode = 'development' | 'staging' | 'production';
|
|
6
|
+
|
|
7
|
+
export type CommonFileConfigLoad = <T>(load: string, mode: Mode, name: string, ignore?: boolean) => Promise<T>;
|
|
8
|
+
|
|
9
|
+
export type CommonFileConfig = {
|
|
10
|
+
load: CommonFileConfigLoad,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// lib/common/file/read.js
|
|
14
|
+
|
|
15
|
+
export type CommonFileReadJson = <T>(src: string) => Promise<T>;
|
|
16
|
+
export type CommonFileReadYaml = <T>(src: string) => Promise<T>;
|
|
17
|
+
|
|
18
|
+
export type CommonFileRead = {
|
|
19
|
+
json: CommonFileReadJson,
|
|
20
|
+
yaml: CommonFileReadYaml,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// lib/common/file/index.js
|
|
24
|
+
|
|
25
|
+
export type CommonFile = {
|
|
26
|
+
config: CommonFileConfig,
|
|
27
|
+
read : CommonFileRead,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// lib/common/init/credential.js
|
|
31
|
+
|
|
32
|
+
export type Credential<T = DataObject> = T;
|
|
33
|
+
// ato de yoi kanji ni shusei shimasu.
|
|
34
|
+
export type RequestStaticCredential = <T>() => Promise<Credential<T>>;
|
|
35
|
+
export type RequestCredential = <T>(key: string) => Promise<Credential<T>>;
|
|
36
|
+
|
|
37
|
+
export type Credentials = {
|
|
38
|
+
module: string, keys: string[], [key: string]: any,
|
|
39
|
+
|
|
40
|
+
requestStaticCredential: RequestStaticCredential,
|
|
41
|
+
requestCredential: RequestCredential,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type CommonInitCredential = <T, U>(config: T, dep: U) => Promise<Credentials>;
|
|
45
|
+
|
|
46
|
+
// lib/common/init/emitter.js
|
|
47
|
+
|
|
48
|
+
export type On = (event: string, ...args: any[]) => void;
|
|
49
|
+
export type Off = (event: string, ...args: any[]) => void;
|
|
50
|
+
export type Once = (event: string, ...args: any[]) => void;
|
|
51
|
+
export type Emit = (event: string, ...args: any[]) => boolean;
|
|
52
|
+
|
|
53
|
+
export type Emitter = {
|
|
54
|
+
on: On, off: Off, once: Once, emit: Emit,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type CommonInitEmitter = () => Emitter;
|
|
58
|
+
|
|
59
|
+
// lib/common/init/log.js
|
|
60
|
+
|
|
61
|
+
export type Log = <T>(o: T) => void;
|
|
62
|
+
export type CommonInitLog = <T>(label: T, indent?: boolean, out?: 'log' | 'error') => Log;
|
|
63
|
+
|
|
64
|
+
// lib/common/init/index.js
|
|
65
|
+
|
|
66
|
+
export type CommonInit = {
|
|
67
|
+
credential: CommonInitCredential, emitter: CommonInitEmitter, log: CommonInitLog,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// lib/common/util/cast.js
|
|
71
|
+
|
|
72
|
+
export type CommonUtilCast = (value: string) => Date | string;
|
|
73
|
+
|
|
74
|
+
// lib/common/util/date.js
|
|
75
|
+
|
|
76
|
+
export type CommonUtilDateIso8601 = string;
|
|
77
|
+
|
|
78
|
+
export type CommonUtilDateIsIsoString = (v: string) => boolean;
|
|
79
|
+
export type CommonUtilDateIsString = (v: string) => boolean;
|
|
80
|
+
export type CommonUtilDateIsDate = (v: Date | string) => boolean;
|
|
81
|
+
export type CommonUtilDateString = (v: Date) => string;
|
|
82
|
+
|
|
83
|
+
export type CommonUtilDate = {
|
|
84
|
+
iso8601: CommonUtilDateIso8601,
|
|
85
|
+
|
|
86
|
+
isIsoString: CommonUtilDateIsIsoString,
|
|
87
|
+
isString: CommonUtilDateIsString,
|
|
88
|
+
isDate: CommonUtilDateIsDate,
|
|
89
|
+
string: CommonUtilDateString,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// lib/common/util/format.js
|
|
93
|
+
|
|
94
|
+
export type CommonUtilFormatPascal = (word: string) => string;
|
|
95
|
+
export type CommonUtilFormatCamel = (word: string) => string;
|
|
96
|
+
export type CommonUtilFormatSnake = (word: string) => string;
|
|
97
|
+
export type CommonUtilFormatKebab = (word: string) => string;
|
|
98
|
+
// pascal
|
|
99
|
+
export type CommonUtilFormatPascalToCamel = (word: string) => string;
|
|
100
|
+
export type CommonUtilFormatPascalToSnake = (word: string) => string;
|
|
101
|
+
export type CommonUtilFormatPascalToKebab = (word: string) => string;
|
|
102
|
+
// camel
|
|
103
|
+
export type CommonUtilFormatCamelToPascal = (word: string) => string;
|
|
104
|
+
export type CommonUtilFormatCamelToSnake = (word: string) => string;
|
|
105
|
+
export type CommonUtilFormatCamelToKebab = (word: string) => string;
|
|
106
|
+
// snake
|
|
107
|
+
export type CommonUtilFormatSnakeToPascal = (word: string) => string;
|
|
108
|
+
export type CommonUtilFormatSnakeToCamel = (word: string) => string;
|
|
109
|
+
export type CommonUtilFormatSnakeToKebab = (word: string) => string;
|
|
110
|
+
// kebab
|
|
111
|
+
export type CommonUtilFormatKebabToPascal = (word: string) => string;
|
|
112
|
+
export type CommonUtilFormatKebabToCamel = (word: string) => string;
|
|
113
|
+
export type CommonUtilFormatKebabToSnake = (word: string) => string;
|
|
114
|
+
// keys
|
|
115
|
+
export type CommonUtilFormatPascalKeys = <T, U>(data: T) => U;
|
|
116
|
+
export type CommonUtilFormatCamelKeys = <T, U>(data: T) => U;
|
|
117
|
+
export type CommonUtilFormatSnakeKeys = <T, U>(data: T) => U;
|
|
118
|
+
export type CommonUtilFormatKebabKeys = <T, U>(data: T) => U;
|
|
119
|
+
|
|
120
|
+
export type CommonUtilFormat = {
|
|
121
|
+
// case
|
|
122
|
+
pascal: CommonUtilFormatPascal,
|
|
123
|
+
camel: CommonUtilFormatCamel,
|
|
124
|
+
snake: CommonUtilFormatSnake,
|
|
125
|
+
kebab: CommonUtilFormatKebab,
|
|
126
|
+
// pascal
|
|
127
|
+
pascalToCamel: CommonUtilFormatPascalToCamel,
|
|
128
|
+
pascalToSnake: CommonUtilFormatPascalToSnake,
|
|
129
|
+
pascalToKebab: CommonUtilFormatPascalToKebab,
|
|
130
|
+
// camel
|
|
131
|
+
camelToPascal: CommonUtilFormatCamelToPascal,
|
|
132
|
+
camelToSnake: CommonUtilFormatCamelToSnake,
|
|
133
|
+
camelToKebab: CommonUtilFormatCamelToKebab,
|
|
134
|
+
// snake
|
|
135
|
+
snakeToPascal: CommonUtilFormatSnakeToPascal,
|
|
136
|
+
snakeToCamel: CommonUtilFormatSnakeToCamel,
|
|
137
|
+
snakeToKebab: CommonUtilFormatSnakeToKebab,
|
|
138
|
+
// kebab
|
|
139
|
+
kebabToPascal: CommonUtilFormatKebabToPascal,
|
|
140
|
+
kebabToCamel: CommonUtilFormatKebabToCamel,
|
|
141
|
+
kebabToSnake: CommonUtilFormatKebabToSnake,
|
|
142
|
+
// keys
|
|
143
|
+
pascalKeys: CommonUtilFormatPascalKeys,
|
|
144
|
+
camelKeys: CommonUtilFormatCamelKeys,
|
|
145
|
+
snakeKeys: CommonUtilFormatSnakeKeys,
|
|
146
|
+
kebabKeys: CommonUtilFormatKebabKeys,
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
// lib/common/util/url.js
|
|
150
|
+
|
|
151
|
+
export type SocketUrlString = string;
|
|
152
|
+
|
|
153
|
+
export type SocketUrlConfig = {
|
|
154
|
+
url: string, [option: string]: string,
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
export type SocketUrlPreObject = {
|
|
158
|
+
url: string, group: string, user: string, [option: string]: string,
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
export type SocketUrlObject = SocketUrlPreObject & {app: string};
|
|
162
|
+
|
|
163
|
+
export type CommonUtilUrlSocketParse = (string: SocketUrlString) => SocketUrlObject;
|
|
164
|
+
export type CommonUtilUrlSocketString = (object: SocketUrlPreObject) => SocketUrlObject;
|
|
165
|
+
export type CommonUtilUrlSocketEither = (config: SocketUrlConfig) => SocketUrlObject;
|
|
166
|
+
|
|
167
|
+
export type CommonUtilUrlSocket = {
|
|
168
|
+
parse: CommonUtilUrlSocketParse,string: CommonUtilUrlSocketString, either: CommonUtilUrlSocketEither,
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
export type CommonUtilUrl = {
|
|
172
|
+
socket: CommonUtilUrlSocket,
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
// lib/common/util/index.js
|
|
176
|
+
|
|
177
|
+
export type CommonUtil = {
|
|
178
|
+
cast: CommonUtilCast, date: CommonUtilDate, format: CommonUtilFormat, url: CommonUtilUrl,
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// lib/common/index.js
|
|
182
|
+
|
|
183
|
+
export type Common = {
|
|
184
|
+
file: CommonFile, init: CommonInit, util: CommonUtil,
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// const
|
|
188
|
+
export declare const common: Common;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// where condition query
|
|
2
|
+
export type ConditionSelect = string[];
|
|
3
|
+
export type Condition = {[key: string]: any};
|
|
4
|
+
|
|
5
|
+
export type ConditionString = string;
|
|
6
|
+
|
|
7
|
+
export type ConditionObject = {
|
|
8
|
+
select?: ConditionSelect, where?: Condition, order?: Condition, limit?: Condition,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type ConditionQuery = ConditionObject | ConditionString;
|
|
12
|
+
|
|
13
|
+
// Cq
|
|
14
|
+
export type CqParse = (string: ConditionString, ignore?: string) => ConditionObject;
|
|
15
|
+
export type CqString = (object: ConditionObject) => ConditionString;
|
|
16
|
+
|
|
17
|
+
export type CqOperators = ('=' | '!' | '<' | '>' | '<=' | '>=' | | '-' | '*'| '!*')[];
|
|
18
|
+
|
|
19
|
+
// deprecated
|
|
20
|
+
//type WolL = 'select' | 'where' | 'order' | 'limit';
|
|
21
|
+
//type WolS = 's' | 'w' | 'o' | 'l';
|
|
22
|
+
|
|
23
|
+
//export type CqKey = {[key in WolL | WolS]: string};
|
|
24
|
+
//export type CqR = {[key in WolS]: string};
|
|
25
|
+
|
|
26
|
+
//export type Cq = {
|
|
27
|
+
//operators: CqOperators, key: CqKey, r: CqR, parse: CqParse, string: CqString,
|
|
28
|
+
//};
|
|
29
|
+
|
|
30
|
+
export type Cq = {
|
|
31
|
+
operators: CqOperators, key: CqKey, r: CqR, parse: CqParse, string: CqString,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// const
|
|
35
|
+
export declare const cq: Cq;
|
|
36
|
+
|
|
37
|
+
// shortcut
|
|
38
|
+
|
|
39
|
+
//export type CO = ConditionObject;
|
|
40
|
+
//export type CS = ConditionString;
|
|
41
|
+
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ConditionObject, CqOperator } from './cq.js';
|
|
2
|
+
|
|
3
|
+
// where data array
|
|
4
|
+
type Data = string | number | boolean | Date | null | undefined;
|
|
5
|
+
|
|
6
|
+
export type DataObject<T = Data> = {
|
|
7
|
+
[k: string | number]: T
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type DataArrayBase<T> = T[];
|
|
11
|
+
export type DataArray<T = DataObject> = DataArrayBase<T>;
|
|
12
|
+
|
|
13
|
+
// Da
|
|
14
|
+
export type DaSplit = (key: string) => [string, string];
|
|
15
|
+
export type DaSplitInit = (operator: CqOperator) => DaSplit;
|
|
16
|
+
|
|
17
|
+
export type DaParse = <T = DataObject>
|
|
18
|
+
(object: any, contentType?: string) => DataArray<T>;
|
|
19
|
+
|
|
20
|
+
export type DaFilter = <T = DataObject>
|
|
21
|
+
(data: DataArray<T>, condition: ConditionObject) => DataArray<T>;
|
|
22
|
+
|
|
23
|
+
export type Da = {
|
|
24
|
+
split: DaSplitInit, parse: DaParse, filter: DaFilter,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// const
|
|
28
|
+
export declare const da: Da;
|
|
29
|
+
|
|
30
|
+
// shortcut
|
|
31
|
+
|
|
32
|
+
//export type DA<T = DataObject> = DataArray<T>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// ConnectionExcption
|
|
2
|
+
export declare class ConnectionException extends Error {
|
|
3
|
+
constructor(message: string);
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// UrlException
|
|
7
|
+
export declare class UrlException extends Error {
|
|
8
|
+
constructor(message: string);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// ServerException
|
|
12
|
+
export declare class ServerException extends Error {
|
|
13
|
+
static header: string; g
|
|
14
|
+
status: any; // henko yotei
|
|
15
|
+
debug?: string;
|
|
16
|
+
constructor(message: number, debug?: string);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// ServerError
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@where-org/where-common",
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Masafumi Sasahara <masafumi@sashara.io> (https://github.com/masafumi-sasahara/)",
|
|
7
|
+
"homepage": "https://where-org.com/",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/where-org/where-common.git"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "index.js",
|
|
14
|
+
"types": "index.d.ts",
|
|
15
|
+
"browser": {
|
|
16
|
+
"./lib/common/index.js": "./lib/common/index-browser.js",
|
|
17
|
+
"./lib/common/init/index.js": "./lib/common/init/index-browser.js"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"js-yaml": "^4.1.0"
|
|
21
|
+
}
|
|
22
|
+
}
|