@book000/node-utils 1.2.30 → 1.2.35

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 (40) hide show
  1. package/dist/tsconfig.build.tsbuildinfo +1 -0
  2. package/dist/tsconfig.tsbuildinfo +1 -1
  3. package/package.json +3 -4
  4. package/.ctiignore +0 -3
  5. package/.eslintignore +0 -5
  6. package/.eslintrc.yml +0 -21
  7. package/.github/FUNDING.yml +0 -1
  8. package/.github/workflows/nodejs-ci.yml +0 -18
  9. package/.github/workflows/release.yml +0 -68
  10. package/.gitignore +0 -130
  11. package/.node-version +0 -1
  12. package/.prettierrc.yml +0 -12
  13. package/dist/examples/example-configuration.d.ts +0 -6
  14. package/dist/examples/example-configuration.d.ts.map +0 -1
  15. package/dist/examples/example-configuration.js +0 -29
  16. package/dist/examples/example-configuration.js.map +0 -1
  17. package/dist/examples/example-discord.d.ts +0 -2
  18. package/dist/examples/example-discord.d.ts.map +0 -1
  19. package/dist/examples/example-discord.js +0 -27
  20. package/dist/examples/example-discord.js.map +0 -1
  21. package/dist/examples/example-logger.d.ts +0 -2
  22. package/dist/examples/example-logger.d.ts.map +0 -1
  23. package/dist/examples/example-logger.js +0 -10
  24. package/dist/examples/example-logger.js.map +0 -1
  25. package/dist/examples/main.d.ts +0 -2
  26. package/dist/examples/main.d.ts.map +0 -1
  27. package/dist/examples/main.js +0 -37
  28. package/dist/examples/main.js.map +0 -1
  29. package/renovate.json +0 -12
  30. package/src/configuration.ts +0 -111
  31. package/src/cycle.d.ts +0 -4
  32. package/src/discord.ts +0 -219
  33. package/src/examples/example-configuration.ts +0 -33
  34. package/src/examples/example-discord.ts +0 -28
  35. package/src/examples/example-logger.ts +0 -6
  36. package/src/examples/main.ts +0 -38
  37. package/src/index.ts +0 -3
  38. package/src/logger.ts +0 -168
  39. package/tsconfig.json +0 -32
  40. package/yarn.lock +0 -3323
@@ -1 +0,0 @@
1
- {"version":3,"file":"example-discord.js","sourceRoot":"","sources":["../../src/examples/example-discord.ts"],"names":[],"mappings":";;;AAAA,0BAAoC;AAE7B,KAAK,UAAU,cAAc;IAClC,MAAM,MAAM,GAAG,UAAM,CAAC,SAAS,CAAC,gBAAgB,CAAC,CAAA;IAEjD,MAAM,iBAAiB,GAAG,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAA;IACzD,IAAI,CAAC,iBAAiB,EAAE;QACtB,MAAM,CAAC,KAAK,CAAC,kCAAkC,CAAC,CAAA;QAChD,OAAM;KACP;IAED,MAAM,OAAO,GAAG,IAAI,WAAO,CAAC;QAC1B,UAAU,EAAE,iBAAiB;KAC9B,CAAC,CAAA;IAEF,MAAM,OAAO,CAAC,WAAW,CAAC,cAAc,CAAC,CAAA;IAEzC,MAAM,OAAO,CAAC,WAAW,CAAC;QACxB,MAAM,EAAE;YACN;gBACE,KAAK,EAAE,cAAc;gBACrB,WAAW,EACT,+bAA+b;gBACjc,KAAK,EAAE,KAAU;aAClB;SACF;KACF,CAAC,CAAA;AACJ,CAAC;AAzBD,wCAyBC"}
@@ -1,2 +0,0 @@
1
- export declare function exampleLogger(): void;
2
- //# sourceMappingURL=example-logger.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"example-logger.d.ts","sourceRoot":"","sources":["../../src/examples/example-logger.ts"],"names":[],"mappings":"AAEA,wBAAgB,aAAa,SAG5B"}
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.exampleLogger = void 0;
4
- const __1 = require("..");
5
- function exampleLogger() {
6
- const logger = __1.Logger.configure('exampleLogger');
7
- logger.info('Hello world!');
8
- }
9
- exports.exampleLogger = exampleLogger;
10
- //# sourceMappingURL=example-logger.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"example-logger.js","sourceRoot":"","sources":["../../src/examples/example-logger.ts"],"names":[],"mappings":";;;AAAA,0BAA2B;AAE3B,SAAgB,aAAa;IAC3B,MAAM,MAAM,GAAG,UAAM,CAAC,SAAS,CAAC,eAAe,CAAC,CAAA;IAChD,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;AAC7B,CAAC;AAHD,sCAGC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=main.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../src/examples/main.ts"],"names":[],"mappings":""}
@@ -1,37 +0,0 @@
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
- const logger_1 = require("@/logger");
7
- const example_configuration_1 = require("./example-configuration");
8
- const example_logger_1 = require("./example-logger");
9
- const example_discord_1 = require("./example-discord");
10
- const node_fs_1 = __importDefault(require("node:fs"));
11
- const node_os_1 = __importDefault(require("node:os"));
12
- async function main() {
13
- const logger = logger_1.Logger.configure('main');
14
- logger.info('Running main()');
15
- logger.info('Calling exampleLogger()');
16
- (0, example_logger_1.exampleLogger)();
17
- logger.info('Create dummy configuration file');
18
- const config = {
19
- foo: 'foo',
20
- bar: 123,
21
- };
22
- const temporaryConfigPath = `${node_os_1.default.tmpdir()}/config.json`;
23
- node_fs_1.default.writeFileSync(temporaryConfigPath, JSON.stringify(config));
24
- logger.info('Set environment variable');
25
- process.env.CONFIG_PATH = temporaryConfigPath;
26
- logger.info('Calling exampleConfiguration()');
27
- (0, example_configuration_1.exampleConfiguration)();
28
- logger.info('Remove dummy configuration file');
29
- node_fs_1.default.unlinkSync(temporaryConfigPath);
30
- logger.info('Calling exampleDiscord()');
31
- await (0, example_discord_1.exampleDiscord)();
32
- }
33
- ;
34
- (async () => {
35
- await main();
36
- })();
37
- //# sourceMappingURL=main.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"main.js","sourceRoot":"","sources":["../../src/examples/main.ts"],"names":[],"mappings":";;;;;AAAA,qCAAiC;AACjC,mEAA6E;AAC7E,qDAAgD;AAChD,uDAAkD;AAClD,sDAAwB;AACxB,sDAAwB;AAExB,KAAK,UAAU,IAAI;IACjB,MAAM,MAAM,GAAG,eAAM,CAAC,SAAS,CAAC,MAAM,CAAC,CAAA;IACvC,MAAM,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAA;IAE7B,MAAM,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAA;IACtC,IAAA,8BAAa,GAAE,CAAA;IAEf,MAAM,CAAC,IAAI,CAAC,iCAAiC,CAAC,CAAA;IAC9C,MAAM,MAAM,GAAkB;QAC5B,GAAG,EAAE,KAAK;QACV,GAAG,EAAE,GAAG;KACT,CAAA;IACD,MAAM,mBAAmB,GAAG,GAAG,iBAAE,CAAC,MAAM,EAAE,cAAc,CAAA;IACxD,iBAAE,CAAC,aAAa,CAAC,mBAAmB,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAA;IAE7D,MAAM,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAA;IACvC,OAAO,CAAC,GAAG,CAAC,WAAW,GAAG,mBAAmB,CAAA;IAE7C,MAAM,CAAC,IAAI,CAAC,gCAAgC,CAAC,CAAA;IAC7C,IAAA,4CAAoB,GAAE,CAAA;IAEtB,MAAM,CAAC,IAAI,CAAC,iCAAiC,CAAC,CAAA;IAC9C,iBAAE,CAAC,UAAU,CAAC,mBAAmB,CAAC,CAAA;IAElC,MAAM,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAA;IACvC,MAAM,IAAA,gCAAc,GAAE,CAAA;AACxB,CAAC;AAED,CAAC;AAAA,CAAC,KAAK,IAAI,EAAE;IACX,MAAM,IAAI,EAAE,CAAA;AACd,CAAC,CAAC,EAAE,CAAA"}
package/renovate.json DELETED
@@ -1,12 +0,0 @@
1
- {
2
- "extends": [
3
- "config:base"
4
- ],
5
- "ignorePresets": [
6
- ":prHourlyLimit2"
7
- ],
8
- "timezone": "Asia/Tokyo",
9
- "dependencyDashboard": false,
10
- "automerge": true,
11
- "branchConcurrentLimit": 0
12
- }
@@ -1,111 +0,0 @@
1
- import fs from 'node:fs'
2
- import { parse } from 'jsonc-parser'
3
-
4
- /**
5
- * 設定ファイルを管理するフレームワーククラス
6
- *
7
- * 設定ファイルは JSONC 形式でパースされる。
8
- *
9
- * @template IConfig 設定ファイルの型
10
- */
11
- export abstract class ConfigFramework<IConfig> {
12
- /** 設定ファイルのパス */
13
- private path: string
14
- private config: IConfig | undefined
15
- private validateFailures: string[] = []
16
-
17
- /**
18
- * コンストラクタ
19
- *
20
- * ファイルの読み込みは行わない。{@link load} を呼び出すことで読み込む。
21
- *
22
- * 原則、以下の環境変数を利用する。値の場所にファイルがあればそれを利用する。
23
- * - CONFIG_PATH
24
- * - CONFIG_FILE
25
- * 環境変数に値が設定されておらず、設定ファイルのパスが path に指定されていてそのファイルがある場合はそのファイルを設定ファイルとして使用する
26
- * いずれの方法でもパスを取得できない場合はエラーを投げる
27
- *
28
- * @param path 設定ファイルのパス
29
- * @returns インスタンス
30
- */
31
- constructor(path?: string) {
32
- const paths = [
33
- process.env.CONFIG_PATH,
34
- process.env.CONFIG_FILE,
35
- path,
36
- ].filter((p) => p !== undefined && fs.existsSync(p)) as string[]
37
- if (paths.length === 0) {
38
- throw new Error('Config path not found')
39
- }
40
- this.path = paths[0]
41
- }
42
-
43
- /**
44
- * 設定ファイルの検証ルールを定義する
45
- *
46
- * key には検証ルールの名前を指定する。
47
- * value には検証ルールを定義する関数を指定する。
48
- *
49
- * @returns 検証ルール
50
- */
51
- protected abstract validates(): {
52
- [key: string]: (config: IConfig) => boolean
53
- }
54
-
55
- /**
56
- * 設定ファイルを読み込む
57
- *
58
- * 設定ファイルのパスはコンストラクタで指定されたものが利用される。
59
- * この関数内ではバリデーションは行わないので、{@link validate} を呼び出す必要がある。
60
- */
61
- public load(): void {
62
- const data = fs.readFileSync(this.path, 'utf8')
63
- const json = parse(data) as IConfig
64
- this.config = json
65
- }
66
-
67
- /**
68
- * 設定ファイルのバリデーションを行う
69
- *
70
- * この関数を呼び出す前に、{@link load} を呼び出して設定ファイルを読み込んでおく必要がある。
71
- * バリデーションに失敗した場合は、{@link getValidateFailures} で失敗した項目を取得できる。
72
- *
73
- * @returns バリデーションに成功した場合は true、失敗した場合は false
74
- */
75
- public validate(): boolean {
76
- if (!this.config) throw new Error('Config not loaded')
77
-
78
- this.validateFailures = []
79
- const validates = this.validates()
80
- for (const key in validates) {
81
- try {
82
- if (!validates[key](this.config)) {
83
- this.validateFailures.push(key)
84
- }
85
- } catch {
86
- this.validateFailures.push(key)
87
- }
88
- }
89
- return this.validateFailures.length === 0
90
- }
91
-
92
- /**
93
- * バリデーションに失敗した項目を取得する
94
- *
95
- * @returns バリデーションに失敗した項目の配列
96
- */
97
- public getValidateFailures(): string[] {
98
- return this.validateFailures
99
- }
100
-
101
- /**
102
- * 設定の値を取得する
103
- *
104
- * @param key 設定のキー
105
- * @returns 設定の値
106
- */
107
- public get<T extends keyof IConfig>(key: T): IConfig[T] {
108
- if (!this.config) throw new Error('Config not loaded')
109
- return this.config[key]
110
- }
111
- }
package/src/cycle.d.ts DELETED
@@ -1,4 +0,0 @@
1
- declare module 'cycle' {
2
- export function decycle(object: any): any
3
- export function retrocycle(json: any): any
4
- }
package/src/discord.ts DELETED
@@ -1,219 +0,0 @@
1
- import axios from 'axios'
2
- import FormData from 'form-data'
3
-
4
- interface DiscordBotOptions {
5
- token: string
6
- channelId: string
7
- }
8
-
9
- interface DiscordWebhookOptions {
10
- webhookUrl: string
11
- }
12
-
13
- export type DiscordOptions = DiscordBotOptions | DiscordWebhookOptions
14
- export interface DiscordEmbedFooter {
15
- text: string
16
- icon_url?: string
17
- proxy_icon_url?: string
18
- }
19
-
20
- export interface DiscordEmbedImage {
21
- url?: string
22
- proxy_url?: string
23
- height?: number
24
- width?: number
25
- }
26
-
27
- export interface DiscordEmbedThumbnail {
28
- url?: string
29
- proxy_url?: string
30
- height?: number
31
- width?: number
32
- }
33
-
34
- export interface DiscordEmbedVideo {
35
- url?: string
36
- proxy_url?: string
37
- height?: number
38
- width?: number
39
- }
40
-
41
- export interface DiscordEmbedProvider {
42
- name?: string
43
- url?: string
44
- }
45
-
46
- export interface DiscordEmbedAuthor {
47
- name?: string
48
- url?: string
49
- icon_url?: string
50
- proxy_icon_url?: string
51
- }
52
-
53
- export interface DiscordEmbedField {
54
- name: string
55
- value: string
56
- inline?: boolean
57
- }
58
-
59
- export interface DiscordEmbed {
60
- title?: string
61
- type?: 'rich' | 'image' | 'video' | 'gifv' | 'article' | 'link'
62
- description?: string
63
- url?: string
64
- timestamp?: string
65
- color?: number
66
- footer?: DiscordEmbedFooter
67
- image?: DiscordEmbedImage
68
- thumbnail?: DiscordEmbedThumbnail
69
- video?: DiscordEmbedVideo
70
- provider?: DiscordEmbedProvider
71
- author?: DiscordEmbedAuthor
72
- fields?: DiscordEmbedField[]
73
- }
74
-
75
- interface DiscordNormalMessage {
76
- content: string
77
- }
78
-
79
- interface DiscordEmbedMessage {
80
- embeds: DiscordEmbed[]
81
- }
82
-
83
- interface DiscordFile {
84
- name: string
85
- file: ArrayBuffer
86
- contentType?: string
87
- isSpoiler?: boolean
88
- }
89
-
90
- interface DiscordFileMessage {
91
- file: DiscordFile
92
- }
93
-
94
- export type DiscordMessage =
95
- | DiscordNormalMessage
96
- | DiscordEmbedMessage
97
- | DiscordFileMessage
98
-
99
- export class Discord {
100
- private options: DiscordOptions
101
-
102
- constructor(options: DiscordOptions) {
103
- // token があれば Bot として動作する
104
- // webhookUrl と channelId があれば Webhook として動作する
105
- // どちらもなければエラーを投げる
106
-
107
- if (this.isDiscordBotOptions(options)) {
108
- this.options = options
109
- } else if (this.isDiscordWebhookOptions(options)) {
110
- this.options = options
111
- } else {
112
- throw new Error('Invalid options')
113
- }
114
- }
115
-
116
- public static get validations(): {
117
- [key: string]: (options: any) => boolean
118
- } {
119
- return {
120
- 'token or webhookUrl and channelId': (options: any) =>
121
- 'token' in options ||
122
- ('webhookUrl' in options && 'channelId' in options),
123
- 'token is valid': (options: any) => typeof options.token === 'string',
124
- 'webhookUrl is valid': (options: any) =>
125
- typeof options.webhookUrl === 'string',
126
- 'channelId is valid': (options: any) =>
127
- typeof options.channelId === 'string',
128
- }
129
- }
130
-
131
- public async sendMessage(message: string | DiscordMessage): Promise<void> {
132
- const formData = new FormData()
133
-
134
- if (typeof message === 'string') {
135
- formData.append('payload_json', JSON.stringify({ content: message }))
136
- } else {
137
- formData.append(
138
- 'payload_json',
139
- JSON.stringify({
140
- content: 'content' in message ? message.content : undefined,
141
- embeds: 'embeds' in message ? message.embeds : undefined,
142
- })
143
- )
144
-
145
- if ('file' in message) {
146
- formData.append('file', message.file.file, {
147
- filename: `${message.file.isSpoiler === true ? 'SPOILER_' : ''}${
148
- message.file.name
149
- }`,
150
- contentType: message.file.contentType,
151
- })
152
- }
153
- }
154
-
155
- await (this.isDiscordBotOptions(this.options)
156
- ? this.sendBot(formData)
157
- : this.sendWebhook(formData))
158
- }
159
-
160
- private async sendBot(formData: FormData): Promise<void> {
161
- if (!this.isDiscordBotOptions(this.options)) {
162
- throw new Error('Invalid bot options')
163
- }
164
-
165
- const response = await axios.post(
166
- `https://discord.com/api/channels/${this.options}/messages`,
167
- formData,
168
- {
169
- headers: {
170
- ...formData.getHeaders(),
171
- Authorization: `Bot ${this.options.token}`,
172
- },
173
- validateStatus: () => true,
174
- }
175
- )
176
- if (response.status !== 200) {
177
- throw new Error(`Discord API returned ${response.status}`)
178
- }
179
- }
180
-
181
- private async sendWebhook(formData: FormData): Promise<void> {
182
- if (!this.isDiscordWebhookOptions(this.options)) {
183
- throw new Error('Invalid webhook options')
184
- }
185
-
186
- const response = await axios.post(this.options.webhookUrl, formData, {
187
- headers: {
188
- ...formData.getHeaders(),
189
- },
190
- validateStatus: () => true,
191
- })
192
- if (response.status !== 200 && response.status !== 204) {
193
- throw new Error(`Discord API returned ${response.status}`)
194
- }
195
- }
196
-
197
- private isDiscordBotOptions(
198
- options: DiscordOptions
199
- ): options is DiscordBotOptions {
200
- return (
201
- 'token' in options &&
202
- typeof options.token === 'string' &&
203
- options.token.length > 0 &&
204
- 'channelId' in options &&
205
- typeof options.channelId === 'string' &&
206
- options.channelId.length > 0
207
- )
208
- }
209
-
210
- private isDiscordWebhookOptions(
211
- options: DiscordOptions
212
- ): options is DiscordWebhookOptions {
213
- return (
214
- 'webhookUrl' in options &&
215
- typeof options.webhookUrl === 'string' &&
216
- options.webhookUrl.length > 0
217
- )
218
- }
219
- }
@@ -1,33 +0,0 @@
1
- import { ConfigFramework, Logger } from '..'
2
-
3
- export interface Configuration {
4
- foo: string
5
- bar: number
6
- }
7
-
8
- class ExampleConfiguration extends ConfigFramework<Configuration> {
9
- protected validates(): { [key: string]: (config: Configuration) => boolean } {
10
- return {
11
- // ...Discord.validations, // When using a message transmission to Discord
12
- 'foo is required': (config) => config.foo !== undefined,
13
- 'foo is string': (config) => typeof config.foo === 'string',
14
- 'foo is 3 or more characters': (config) => config.foo.length >= 3,
15
- 'bar is required': (config) => config.bar !== undefined,
16
- 'bar is number': (config) => typeof config.bar === 'number',
17
- }
18
- }
19
- }
20
-
21
- export function exampleConfiguration() {
22
- const logger = Logger.configure('exampleConfiguration')
23
- const config = new ExampleConfiguration()
24
- config.load()
25
- if (!config.validate()) {
26
- logger.error('Configuration validation failed')
27
- logger.error(config.getValidateFailures().join(', '))
28
- return
29
- }
30
-
31
- logger.info(`foo: ${config.get('foo')}`)
32
- logger.info(`bar: ${config.get('bar')}`)
33
- }
@@ -1,28 +0,0 @@
1
- import { Discord, Logger } from '..'
2
-
3
- export async function exampleDiscord() {
4
- const logger = Logger.configure('exampleDiscord')
5
-
6
- const discordWebhookUrl = process.env.DISCORD_WEBHOOK_URL
7
- if (!discordWebhookUrl) {
8
- logger.error('DISCORD_WEBHOOK_URL are required')
9
- return
10
- }
11
-
12
- const discord = new Discord({
13
- webhookUrl: discordWebhookUrl,
14
- })
15
-
16
- await discord.sendMessage('Hello world!')
17
-
18
- await discord.sendMessage({
19
- embeds: [
20
- {
21
- title: 'Hello world!',
22
- description:
23
- 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
24
- color: 0x00_ff_00,
25
- },
26
- ],
27
- })
28
- }
@@ -1,6 +0,0 @@
1
- import { Logger } from '..'
2
-
3
- export function exampleLogger() {
4
- const logger = Logger.configure('exampleLogger')
5
- logger.info('Hello world!')
6
- }
@@ -1,38 +0,0 @@
1
- import { Logger } from '@/logger'
2
- import { Configuration, exampleConfiguration } from './example-configuration'
3
- import { exampleLogger } from './example-logger'
4
- import { exampleDiscord } from './example-discord'
5
- import fs from 'node:fs'
6
- import os from 'node:os'
7
-
8
- async function main() {
9
- const logger = Logger.configure('main')
10
- logger.info('Running main()')
11
-
12
- logger.info('Calling exampleLogger()')
13
- exampleLogger()
14
-
15
- logger.info('Create dummy configuration file')
16
- const config: Configuration = {
17
- foo: 'foo',
18
- bar: 123,
19
- }
20
- const temporaryConfigPath = `${os.tmpdir()}/config.json`
21
- fs.writeFileSync(temporaryConfigPath, JSON.stringify(config))
22
-
23
- logger.info('Set environment variable')
24
- process.env.CONFIG_PATH = temporaryConfigPath
25
-
26
- logger.info('Calling exampleConfiguration()')
27
- exampleConfiguration()
28
-
29
- logger.info('Remove dummy configuration file')
30
- fs.unlinkSync(temporaryConfigPath)
31
-
32
- logger.info('Calling exampleDiscord()')
33
- await exampleDiscord()
34
- }
35
-
36
- ;(async () => {
37
- await main()
38
- })()
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './configuration'
2
- export * from './discord'
3
- export * from './logger'