@dchighs/dc-config 0.0.10 → 0.1.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.
- package/LICENSE +20 -20
- package/README.md +52 -28
- package/dist/dc-config.d.ts +21 -0
- package/dist/dc-config.js +45 -0
- package/package.json +38 -38
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 Marcuth
|
|
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
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Marcuth
|
|
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
21
|
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -10,13 +10,12 @@ Installation is straightforward—simply use your preferred package manager. Her
|
|
|
10
10
|
|
|
11
11
|
```cmd
|
|
12
12
|
npm i @dchighs/dc-config
|
|
13
|
-
|
|
14
13
|
```
|
|
15
14
|
|
|
16
15
|
## 🚀 Usage
|
|
17
16
|
|
|
18
17
|
<a href="https://www.buymeacoffee.com/marcuth">
|
|
19
|
-
|
|
18
|
+
<img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" width="200">
|
|
20
19
|
</a>
|
|
21
20
|
|
|
22
21
|
### Fetching configuration for specific parameters
|
|
@@ -27,24 +26,23 @@ To create a `Config` instance, you need to provide credentials or use the static
|
|
|
27
26
|
import { Config, ConfigLanguage, ConfigPlatform, ConfigFilter } from "@dchighs/dc-config"
|
|
28
27
|
|
|
29
28
|
;(async () => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
29
|
+
const userId = process.env.USER_ID
|
|
30
|
+
const authToken = process.env.AUTH_TOKEN
|
|
31
|
+
const url = process.env.URL
|
|
32
|
+
|
|
33
|
+
const config = await Config.create({
|
|
34
|
+
url: url,
|
|
35
|
+
userId: userId,
|
|
36
|
+
authToken: authToken,
|
|
37
|
+
language: ConfigLanguage.Turkish, // optional - "en" is default
|
|
38
|
+
platform: ConfigPlatform.Android, // optional - "ios" is default
|
|
39
|
+
filter: [ConfigFilter.Items] // optional - undefined is default
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
const data = config.data
|
|
43
|
+
|
|
44
|
+
console.log(data)
|
|
46
45
|
})();
|
|
47
|
-
|
|
48
46
|
```
|
|
49
47
|
|
|
50
48
|
---
|
|
@@ -58,17 +56,43 @@ import { Config, ConfigLanguage, ConfigPlatform, GameConfigDto } from "@dchighs/
|
|
|
58
56
|
import fs from "node:fs"
|
|
59
57
|
|
|
60
58
|
;(async () => {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
59
|
+
const filePath = "config.json"
|
|
60
|
+
const contentString = await fs.promises.readFile(filePath, { encoding: "utf-8" })
|
|
61
|
+
const data = JSON.parse(contentString) as GameConfigDto
|
|
62
|
+
|
|
63
|
+
const config = new Config({
|
|
64
|
+
language: ConfigLanguage.Turkish,
|
|
65
|
+
platform: ConfigPlatform.Android,
|
|
66
|
+
data: data
|
|
67
|
+
})
|
|
70
68
|
})();
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
### Fetching a raw configuration from a URL
|
|
71
74
|
|
|
75
|
+
If you have a full raw JSON file and want to fetch it and filter specific keys:
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import { Config, ConfigFilter, ConfigLanguage } from "@dchighs/dc-config"
|
|
79
|
+
|
|
80
|
+
;(async () => {
|
|
81
|
+
const rawUrl = "https://example.com/config.json"
|
|
82
|
+
|
|
83
|
+
// Using createRaw to get a Config instance
|
|
84
|
+
const config = await Config.createRaw({
|
|
85
|
+
url: rawUrl,
|
|
86
|
+
filter: [ConfigFilter.Items],
|
|
87
|
+
language: ConfigLanguage.English
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
// Or using fetchRaw to get just the data (GameConfigDto)
|
|
91
|
+
const data = await Config.fetchRaw({
|
|
92
|
+
url: rawUrl,
|
|
93
|
+
filter: [ConfigFilter.Items]
|
|
94
|
+
})
|
|
95
|
+
})();
|
|
72
96
|
```
|
|
73
97
|
|
|
74
98
|
---
|
package/dist/dc-config.d.ts
CHANGED
|
@@ -13,13 +13,34 @@ export type CreateOptions = Omit<Omit<ConfigOptions, "platform" | "language"> &
|
|
|
13
13
|
language?: ConfigLanguage | `${ConfigLanguage}`;
|
|
14
14
|
platform?: ConfigPlatform | `${ConfigPlatform}`;
|
|
15
15
|
}, "data">;
|
|
16
|
+
export type CreateRawOptions = {
|
|
17
|
+
url: string;
|
|
18
|
+
filter?: Array<ConfigFilter | `${ConfigFilter}`>;
|
|
19
|
+
language?: ConfigLanguage | `${ConfigLanguage}`;
|
|
20
|
+
platform?: ConfigPlatform | `${ConfigPlatform}`;
|
|
21
|
+
};
|
|
16
22
|
export type FetchOptions = CreateOptions;
|
|
23
|
+
export type FetchRawOptions = Pick<CreateRawOptions, "url" | "filter">;
|
|
17
24
|
export declare class Config {
|
|
18
25
|
readonly platform: ConfigPlatform;
|
|
19
26
|
readonly langauge: ConfigLanguage;
|
|
20
27
|
readonly filter?: ConfigFilter[];
|
|
21
28
|
readonly data: GameConfigDto;
|
|
22
29
|
constructor({ data, language, platform, filter }: ConfigOptions);
|
|
30
|
+
/**
|
|
31
|
+
* Creates a new Config instance by fetching data from the API with authentication.
|
|
32
|
+
*/
|
|
23
33
|
static create({ authToken, userId, filter, language, platform, url }: CreateOptions): Promise<Config>;
|
|
34
|
+
/**
|
|
35
|
+
* Creates a new Config instance by fetching a raw JSON file from a URL.
|
|
36
|
+
*/
|
|
37
|
+
static createRaw({ url, filter, language, platform }: CreateRawOptions): Promise<Config>;
|
|
38
|
+
/**
|
|
39
|
+
* Fetches configuration from the API with authentication.
|
|
40
|
+
*/
|
|
24
41
|
static fetch({ authToken, userId, filter, language, platform, url }: FetchOptions): Promise<GameConfigDto>;
|
|
42
|
+
/**
|
|
43
|
+
* Fetches a raw config file from a URL and filters its contents.
|
|
44
|
+
*/
|
|
45
|
+
static fetchRaw({ url, filter }: FetchRawOptions): Promise<GameConfigDto>;
|
|
25
46
|
}
|
package/dist/dc-config.js
CHANGED
|
@@ -13,6 +13,9 @@ class Config {
|
|
|
13
13
|
this.platform = platform;
|
|
14
14
|
this.filter = filter ? filter : undefined;
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new Config instance by fetching data from the API with authentication.
|
|
18
|
+
*/
|
|
16
19
|
static async create({ authToken, userId, filter, language, platform, url }) {
|
|
17
20
|
const data = await Config.fetch({
|
|
18
21
|
authToken: authToken,
|
|
@@ -29,6 +32,21 @@ class Config {
|
|
|
29
32
|
platform: platform !== null && platform !== void 0 ? platform : enums_1.ConfigPlatform.Default
|
|
30
33
|
});
|
|
31
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Creates a new Config instance by fetching a raw JSON file from a URL.
|
|
37
|
+
*/
|
|
38
|
+
static async createRaw({ url, filter, language, platform }) {
|
|
39
|
+
const data = await Config.fetchRaw({ url, filter });
|
|
40
|
+
return new Config({
|
|
41
|
+
data: data,
|
|
42
|
+
filter: filter,
|
|
43
|
+
language: language !== null && language !== void 0 ? language : enums_1.ConfigLanguage.Default,
|
|
44
|
+
platform: platform !== null && platform !== void 0 ? platform : enums_1.ConfigPlatform.Default
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Fetches configuration from the API with authentication.
|
|
49
|
+
*/
|
|
32
50
|
static async fetch({ authToken, userId, filter, language, platform, url }) {
|
|
33
51
|
const response = await axios_1.default.post(url, null, {
|
|
34
52
|
params: {
|
|
@@ -42,5 +60,32 @@ class Config {
|
|
|
42
60
|
const data = response.data;
|
|
43
61
|
return data;
|
|
44
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Fetches a raw config file from a URL and filters its contents.
|
|
65
|
+
*/
|
|
66
|
+
static async fetchRaw({ url, filter }) {
|
|
67
|
+
const response = await axios_1.default.get(url);
|
|
68
|
+
const fullData = response.data;
|
|
69
|
+
if (!filter || filter.length === 0) {
|
|
70
|
+
return {
|
|
71
|
+
game_data: {
|
|
72
|
+
config: fullData.game_data.config
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
const filteredData = {};
|
|
77
|
+
filter.forEach((key) => {
|
|
78
|
+
var _a, _b;
|
|
79
|
+
const value = (_b = (_a = fullData.game_data) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b[key];
|
|
80
|
+
if (value !== undefined) {
|
|
81
|
+
filteredData[key] = value;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
return {
|
|
85
|
+
game_data: {
|
|
86
|
+
config: filteredData
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
45
90
|
}
|
|
46
91
|
exports.Config = Config;
|
package/package.json
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@dchighs/dc-config",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "A library to handle configuration for Dragon City.",
|
|
5
|
-
"main": "./dist/index.js",
|
|
6
|
-
"module": "./dist/index.js",
|
|
7
|
-
"types": "./dist/index.d.ts",
|
|
8
|
-
"files": [
|
|
9
|
-
"dist/*",
|
|
10
|
-
"!/**/__tests__"
|
|
11
|
-
],
|
|
12
|
-
"keywords": [
|
|
13
|
-
"dragon city",
|
|
14
|
-
"dc",
|
|
15
|
-
"configuration",
|
|
16
|
-
"dc highs"
|
|
17
|
-
],
|
|
18
|
-
"scripts": {
|
|
19
|
-
"build": "tsc",
|
|
20
|
-
"update-dto": "ts-node scripts/update-dto.ts"
|
|
21
|
-
},
|
|
22
|
-
"author": "Marcuth",
|
|
23
|
-
"license": "MIT",
|
|
24
|
-
"type": "commonjs",
|
|
25
|
-
"devDependencies": {
|
|
26
|
-
"@marcuth/env": "^0.0.1",
|
|
27
|
-
"@types/node": "^25.0.3",
|
|
28
|
-
"quicktype-core": "^23.2.6",
|
|
29
|
-
"ts-node": "^10.9.2",
|
|
30
|
-
"typescript": "^5.9.3"
|
|
31
|
-
},
|
|
32
|
-
"dependencies": {
|
|
33
|
-
"axios": "^1.13.2"
|
|
34
|
-
},
|
|
35
|
-
"peerDependencies": {
|
|
36
|
-
"@dchighs/dc-core": "^0.3.0"
|
|
37
|
-
}
|
|
38
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@dchighs/dc-config",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A library to handle configuration for Dragon City.",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist/*",
|
|
10
|
+
"!/**/__tests__"
|
|
11
|
+
],
|
|
12
|
+
"keywords": [
|
|
13
|
+
"dragon city",
|
|
14
|
+
"dc",
|
|
15
|
+
"configuration",
|
|
16
|
+
"dc highs"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc",
|
|
20
|
+
"update-dto": "ts-node scripts/update-dto.ts"
|
|
21
|
+
},
|
|
22
|
+
"author": "Marcuth",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"type": "commonjs",
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@marcuth/env": "^0.0.1",
|
|
27
|
+
"@types/node": "^25.0.3",
|
|
28
|
+
"quicktype-core": "^23.2.6",
|
|
29
|
+
"ts-node": "^10.9.2",
|
|
30
|
+
"typescript": "^5.9.3"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"axios": "^1.13.2"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@dchighs/dc-core": "^0.3.0"
|
|
37
|
+
}
|
|
38
|
+
}
|