dnf-api 1.1.0 → 1.1.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.
@@ -1,43 +1,43 @@
1
- // 설정 인터페이스
2
- export interface IConfig {
3
- key: string;
4
- hideOnErrorApiKey: boolean;
5
- hideKeyText: string;
6
- timeout: number;
7
- returnJSON: boolean;
8
- responseHeader: boolean;
9
- showURL: boolean;
10
- maxRequestsPerSecond: number;
11
- }
12
-
13
- // 기본 설정값
14
- const defaultConfig: IConfig = {
15
- key: "",
16
- hideOnErrorApiKey: true,
17
- hideKeyText: "{HIDE_KEY}",
18
- timeout: 5000,
19
- returnJSON: false,
20
- responseHeader: false,
21
- showURL: false,
22
- maxRequestsPerSecond: 1000,
23
- };
24
-
25
- // 현재 설정 (복사본)
26
- export const config: IConfig = { ...defaultConfig };
27
-
28
- /**
29
- * 설정을 업데이트합니다.
30
- * @param newConfig 업데이트할 설정 값
31
- */
32
- export function setConfig(newConfig: Partial<IConfig>): void {
33
- Object.assign(config, newConfig);
34
- }
35
-
36
- /**
37
- * 설정을 기본값으로 초기화합니다.
38
- */
39
- export function resetConfig(): void {
40
- Object.assign(config, defaultConfig);
41
- }
42
-
43
- export default config;
1
+ // 설정 인터페이스
2
+ export interface IConfig {
3
+ key: string;
4
+ hideOnErrorApiKey: boolean;
5
+ hideKeyText: string;
6
+ timeout: number;
7
+ returnJSON: boolean;
8
+ responseHeader: boolean;
9
+ showURL: boolean;
10
+ maxRequestsPerSecond: number;
11
+ }
12
+
13
+ // 기본 설정값
14
+ const defaultConfig: IConfig = {
15
+ key: "",
16
+ hideOnErrorApiKey: true,
17
+ hideKeyText: "{HIDE_KEY}",
18
+ timeout: 5000,
19
+ returnJSON: false,
20
+ responseHeader: false,
21
+ showURL: false,
22
+ maxRequestsPerSecond: 1000,
23
+ };
24
+
25
+ // 현재 설정 (복사본)
26
+ export const config: IConfig = { ...defaultConfig };
27
+
28
+ /**
29
+ * 설정을 업데이트합니다.
30
+ * @param newConfig 업데이트할 설정 값
31
+ */
32
+ export function setConfig(newConfig: Partial<IConfig>): void {
33
+ Object.assign(config, newConfig);
34
+ }
35
+
36
+ /**
37
+ * 설정을 기본값으로 초기화합니다.
38
+ */
39
+ export function resetConfig(): void {
40
+ Object.assign(config, defaultConfig);
41
+ }
42
+
43
+ export default config;
package/src/util/index.ts CHANGED
@@ -1,17 +1,17 @@
1
- import config, { setConfig, resetConfig } from "./config";
2
- import * as params from "./params";
3
- import query from "./query";
4
- import { requestQueue } from "./queue";
5
- import { createRequest } from "./request-helper";
6
- import * as staticUtil from "./static";
7
-
8
- export {
9
- config,
10
- setConfig,
11
- resetConfig,
12
- query,
13
- staticUtil,
14
- params,
15
- createRequest,
16
- requestQueue,
17
- };
1
+ import config, { setConfig, resetConfig } from "./config";
2
+ import * as params from "./params";
3
+ import query from "./query";
4
+ import { requestQueue } from "./queue";
5
+ import { createRequest } from "./request-helper";
6
+ import * as staticUtil from "./static";
7
+
8
+ export {
9
+ config,
10
+ setConfig,
11
+ resetConfig,
12
+ query,
13
+ staticUtil,
14
+ params,
15
+ createRequest,
16
+ requestQueue,
17
+ };
@@ -1,95 +1,95 @@
1
- import type * as staticUtil from "./static";
2
-
3
- // 기본 파라미터 타입
4
- export type ParamValue = string | number | boolean | undefined;
5
-
6
- // 인덱스 시그니처가 있는 기본 인터페이스
7
- export interface BaseParams {
8
- [key: string]: ParamValue | ParamValue[] | object | undefined;
9
- }
10
-
11
- export interface QueryOptions<T = BaseParams> {
12
- base: string;
13
- params?: T;
14
- }
15
-
16
- export interface ICharParams extends BaseParams {
17
- characterName?: string;
18
- jobId?: string;
19
- jobGrowId?: string;
20
- isAllJobGrow?: boolean;
21
- wordType?: staticUtil.charactersWordType;
22
- limit?: number;
23
- }
24
-
25
- export interface ITimeLine extends BaseParams {
26
- serverId?: staticUtil.server;
27
- characterId?: string;
28
- startDate?: Date;
29
- endDate?: Date;
30
- limit?: number;
31
- code?: string[];
32
- next?: string;
33
- }
34
-
35
- export interface IAuction extends BaseParams {
36
- limit?: number;
37
- sort?: IAuctionSort;
38
- itemId?: string;
39
- itemName?: string;
40
- wordType?: staticUtil.auctionWordType;
41
- wordShort?: boolean;
42
- q?: IAuctionQuery;
43
- }
44
-
45
- export interface IAuctionSort {
46
- unitPrice?: staticUtil.sort;
47
- reinforce?: staticUtil.sort;
48
- auctionNo?: staticUtil.sort;
49
- }
50
-
51
- export interface IAuctionQuery {
52
- minLevel?: number;
53
- maxLevel?: number;
54
- raity?: staticUtil.rarity;
55
- reinforceTypeId: staticUtil.reinforceType;
56
- minReinforce?: number;
57
- maxReinforce?: number;
58
- minRefine?: number;
59
- maxRefine?: number;
60
- minFame?: number;
61
- maxFame?: number;
62
- }
63
-
64
- export interface IActionSoldOption extends BaseParams {
65
- limit?: number;
66
- wordType?: staticUtil.auctionWordType;
67
- wordShort?: boolean;
68
- itemId?: string;
69
- itemName?: string;
70
- sort?: IAuctionSort;
71
- }
72
-
73
- export interface IItem extends BaseParams {
74
- limit?: number;
75
- itemName?: string;
76
- hashtag?: string[];
77
- wordType?: staticUtil.auctionWordType;
78
- q?: IItemQuery;
79
- }
80
-
81
- export interface IItemQuery {
82
- minLevel?: number;
83
- maxLevel?: number;
84
- rarity?: staticUtil.rarity;
85
- }
86
-
87
- export interface ISetItem extends BaseParams {
88
- setItemName?: string;
89
- limit?: number;
90
- wordType?: staticUtil.auctionWordType;
91
- }
92
-
93
- export interface ISkill extends BaseParams {
94
- jobGrowId: string;
95
- }
1
+ import type * as staticUtil from "./static";
2
+
3
+ // 기본 파라미터 타입
4
+ export type ParamValue = string | number | boolean | undefined;
5
+
6
+ // 인덱스 시그니처가 있는 기본 인터페이스
7
+ export interface BaseParams {
8
+ [key: string]: ParamValue | ParamValue[] | object | undefined;
9
+ }
10
+
11
+ export interface QueryOptions<T = BaseParams> {
12
+ base: string;
13
+ params?: T;
14
+ }
15
+
16
+ export interface ICharParams extends BaseParams {
17
+ characterName?: string;
18
+ jobId?: string;
19
+ jobGrowId?: string;
20
+ isAllJobGrow?: boolean;
21
+ wordType?: staticUtil.charactersWordType;
22
+ limit?: number;
23
+ }
24
+
25
+ export interface ITimeLine extends BaseParams {
26
+ serverId?: staticUtil.server;
27
+ characterId?: string;
28
+ startDate?: Date;
29
+ endDate?: Date;
30
+ limit?: number;
31
+ code?: string[];
32
+ next?: string;
33
+ }
34
+
35
+ export interface IAuction extends BaseParams {
36
+ limit?: number;
37
+ sort?: IAuctionSort;
38
+ itemId?: string;
39
+ itemName?: string;
40
+ wordType?: staticUtil.auctionWordType;
41
+ wordShort?: boolean;
42
+ q?: IAuctionQuery;
43
+ }
44
+
45
+ export interface IAuctionSort {
46
+ unitPrice?: staticUtil.sort;
47
+ reinforce?: staticUtil.sort;
48
+ auctionNo?: staticUtil.sort;
49
+ }
50
+
51
+ export interface IAuctionQuery {
52
+ minLevel?: number;
53
+ maxLevel?: number;
54
+ raity?: staticUtil.rarity;
55
+ reinforceTypeId: staticUtil.reinforceType;
56
+ minReinforce?: number;
57
+ maxReinforce?: number;
58
+ minRefine?: number;
59
+ maxRefine?: number;
60
+ minFame?: number;
61
+ maxFame?: number;
62
+ }
63
+
64
+ export interface IActionSoldOption extends BaseParams {
65
+ limit?: number;
66
+ wordType?: staticUtil.auctionWordType;
67
+ wordShort?: boolean;
68
+ itemId?: string;
69
+ itemName?: string;
70
+ sort?: IAuctionSort;
71
+ }
72
+
73
+ export interface IItem extends BaseParams {
74
+ limit?: number;
75
+ itemName?: string;
76
+ hashtag?: string[];
77
+ wordType?: staticUtil.auctionWordType;
78
+ q?: IItemQuery;
79
+ }
80
+
81
+ export interface IItemQuery {
82
+ minLevel?: number;
83
+ maxLevel?: number;
84
+ rarity?: staticUtil.rarity;
85
+ }
86
+
87
+ export interface ISetItem extends BaseParams {
88
+ setItemName?: string;
89
+ limit?: number;
90
+ wordType?: staticUtil.auctionWordType;
91
+ }
92
+
93
+ export interface ISkill extends BaseParams {
94
+ jobGrowId: string;
95
+ }
package/src/util/query.ts CHANGED
@@ -1,144 +1,147 @@
1
- import consola from "consola";
2
-
3
- import type * as model from "../model";
4
- import * as Util from "./";
5
- import type { BaseParams } from "./params";
6
- import { requestQueue } from "./queue";
7
-
8
- // 요청 옵션 인터페이스
9
- export interface RequestOptions {
10
- base: string;
11
- params?: BaseParams;
12
- url?: string;
13
- }
14
-
15
- const API_BASE_URL = "https://api.neople.co.kr";
16
-
17
- // 파라미터를 URLSearchParams로 변환 (중첩 객체는 JSON으로 직렬화)
18
- const toSearchParams = (params: BaseParams): URLSearchParams => {
19
- const entries: [string, string][] = [];
20
- for (const [key, value] of Object.entries(params)) {
21
- if (value === undefined) continue;
22
- if (typeof value === "object" && !Array.isArray(value)) {
23
- entries.push([key, JSON.stringify(value)]);
24
- } else if (Array.isArray(value)) {
25
- entries.push([key, value.join(",")]);
26
- } else {
27
- entries.push([key, String(value)]);
28
- }
29
- }
30
- return new URLSearchParams(entries);
31
- };
32
-
33
- // HTTP 요청 전송 함수
34
- const sender = async <T>(
35
- path: string,
36
- method: "GET" | "POST",
37
- query: BaseParams,
38
- ): Promise<{
39
- ok: boolean;
40
- status: number;
41
- data: T | model.IDnfResponse<T>;
42
- }> => {
43
- const url = new URL(path, API_BASE_URL);
44
- url.search = toSearchParams(query).toString();
45
-
46
- const res = await fetch(url.toString(), { method });
47
- const data = (await res.json()) as T | model.IDnfResponse<T>;
48
-
49
- return {
50
- ok: res.ok,
51
- status: res.status,
52
- data,
53
- };
54
- };
55
-
56
- // URL에서 API 키 숨김 처리
57
- const showUrl = (url: string): string => {
58
- if (Util.config.key) {
59
- return url?.replace(Util.config.key, Util.config.hideKeyText);
60
- }
61
- return url;
62
- };
63
-
64
- /**
65
- * URI 경로를 빌드합니다.
66
- * @param args 경로 세그먼트들
67
- * @returns 결합된 URI 경로
68
- */
69
- export function UriBuilder(...args: (string | number)[]): string {
70
- return args.join("/");
71
- }
72
-
73
- /**
74
- * 쿼리 문자열을 빌드합니다.
75
- * @param query 쿼리 배열
76
- * @returns 쿼리 문자열
77
- */
78
- export function QueryBuilder(query: (string | number)[]): string {
79
- const qString: string[] = [];
80
- for (const key in query) {
81
- qString.push(`${key}:${query[key]},`);
82
- }
83
- return qString.join(",");
84
- }
85
-
86
- /**
87
- * 던전앤파이터 API 서버에 요청을 보내는 함수입니다.
88
- * @param opt 요청 옵션
89
- * @param method HTTP 메서드
90
- * @returns API 응답
91
- */
92
- export async function Request<T>(
93
- opt: RequestOptions = { base: "" },
94
- method: "GET" | "POST" = "GET",
95
- ): Promise<model.IDnfResponse<T>> {
96
- if (!Util.config.key) {
97
- throw new Error(
98
- "API key is required. Set config.key before making requests.",
99
- );
100
- }
101
-
102
- const params: BaseParams = opt.params ?? {};
103
-
104
- if (params.q && Array.isArray(params.q)) {
105
- params.q = QueryBuilder(params.q as (string | number)[]);
106
- }
107
-
108
- params.apikey = Util.config.key;
109
-
110
- if (Util.config.showURL) {
111
- consola.log(
112
- "request url:",
113
- showUrl(`${opt.base}?${toSearchParams(params)}`),
114
- );
115
- }
116
-
117
- const res = await requestQueue.add(() => sender<T>(opt.base, method, params));
118
-
119
- if (!res.ok) {
120
- const resBody = res.data as model.IDnfResponse<T>;
121
- const error: model.IDnfErrorResponse = {
122
- url: showUrl(opt.url ?? ""),
123
- status: res.status || 0,
124
- statusText: "",
125
- code: resBody.error?.code || "",
126
- message: resBody.error?.message || "",
127
- };
128
- return { error };
129
- }
130
-
131
- return { data: res.data as T };
132
- }
133
-
134
- /**
135
- * 아이템 쿼리를 생성합니다.
136
- * @param query 쿼리 문자열
137
- * @returns 인코딩된 쿼리
138
- */
139
- export function makeItemQuery(query: string): string {
140
- return encodeURI(query);
141
- }
142
-
143
- // 기존 호환성을 위한 default export
144
- export default { UriBuilder, QueryBuilder, Request, makeItemQuery };
1
+ import consola from "consola";
2
+
3
+ import type * as model from "../model";
4
+ import * as Util from "./";
5
+ import type { BaseParams } from "./params";
6
+ import { requestQueue } from "./queue";
7
+
8
+ // 요청 옵션 인터페이스
9
+ export interface RequestOptions {
10
+ base: string;
11
+ params?: BaseParams;
12
+ url?: string;
13
+ }
14
+
15
+ const API_BASE_URL = "https://api.neople.co.kr";
16
+
17
+ // 파라미터를 URLSearchParams로 변환 (중첩 객체는 JSON으로 직렬화)
18
+ const toSearchParams = (params: BaseParams): URLSearchParams => {
19
+ const entries: [string, string][] = [];
20
+ for (const [key, value] of Object.entries(params)) {
21
+ if (value === undefined) continue;
22
+ if (typeof value === "object" && !Array.isArray(value)) {
23
+ entries.push([key, JSON.stringify(value)]);
24
+ } else if (Array.isArray(value)) {
25
+ entries.push([key, value.join(",")]);
26
+ } else {
27
+ entries.push([key, String(value)]);
28
+ }
29
+ }
30
+ return new URLSearchParams(entries);
31
+ };
32
+
33
+ // HTTP 요청 전송 함수
34
+ const sender = async <T>(
35
+ path: string,
36
+ method: "GET" | "POST",
37
+ query: BaseParams
38
+ ): Promise<{
39
+ ok: boolean;
40
+ status: number;
41
+ data: T | model.IDnfResponse<T>;
42
+ }> => {
43
+ const url = new URL(path, API_BASE_URL);
44
+ url.search = toSearchParams(query).toString();
45
+
46
+ const res = await fetch(url.toString(), { method });
47
+ const data = (await res.json()) as T | model.IDnfResponse<T>;
48
+
49
+ return {
50
+ ok: res.ok,
51
+ status: res.status,
52
+ data,
53
+ };
54
+ };
55
+
56
+ // URL에서 API 키 숨김 처리
57
+ const showUrl = (url: string): string => {
58
+ if (Util.config.key) {
59
+ return url?.replace(Util.config.key, Util.config.hideKeyText);
60
+ }
61
+ return url;
62
+ };
63
+
64
+ /**
65
+ * URI 경로를 빌드합니다.
66
+ * @param args 경로 세그먼트들
67
+ * @returns 결합된 URI 경로
68
+ */
69
+ export function UriBuilder(...args: (string | number)[]): string {
70
+ return args.join("/");
71
+ }
72
+
73
+ /**
74
+ * 쿼리 문자열을 빌드합니다.
75
+ * @param query 쿼리 배열
76
+ * @returns 쿼리 문자열
77
+ */
78
+ export function QueryBuilder(query: (string | number)[]): string {
79
+ const qString: string[] = [];
80
+ for (const key in query) {
81
+ qString.push(`${key}:${query[key]},`);
82
+ }
83
+ return qString.join(",");
84
+ }
85
+
86
+ /**
87
+ * 던전앤파이터 API 서버에 요청을 보내는 함수입니다.
88
+ * @param opt 요청 옵션
89
+ * @param method HTTP 메서드
90
+ * @returns API 응답
91
+ */
92
+ export async function Request<T>(
93
+ opt: RequestOptions = { base: "" },
94
+ method: "GET" | "POST" = "GET"
95
+ ): Promise<model.IDnfResponse<T>> {
96
+ if (!Util.config.key) {
97
+ throw new Error(
98
+ "API key is required. Set config.key before making requests."
99
+ );
100
+ }
101
+
102
+ const params: BaseParams = { ...opt.params };
103
+
104
+ if (params.q && Array.isArray(params.q)) {
105
+ params.q = QueryBuilder(params.q as (string | number)[]);
106
+ }
107
+ if (params.sort && Array.isArray(params.sort)) {
108
+ params.sort = QueryBuilder(params.sort as (string | number)[]);
109
+ }
110
+
111
+ params.apikey = Util.config.key;
112
+
113
+ if (Util.config.showURL) {
114
+ consola.log(
115
+ "request url:",
116
+ showUrl(`${opt.base}?${toSearchParams(params)}`)
117
+ );
118
+ }
119
+
120
+ const res = await requestQueue.add(() => sender<T>(opt.base, method, params));
121
+
122
+ if (!res.ok) {
123
+ const resBody = res.data as model.IDnfResponse<T>;
124
+ const error: model.IDnfErrorResponse = {
125
+ url: showUrl(opt.url ?? ""),
126
+ status: res.status || 0,
127
+ statusText: "",
128
+ code: resBody.error?.code || "",
129
+ message: resBody.error?.message || "",
130
+ };
131
+ return { error };
132
+ }
133
+
134
+ return { data: res.data as T };
135
+ }
136
+
137
+ /**
138
+ * 아이템 쿼리를 생성합니다.
139
+ * @param query 쿼리 문자열
140
+ * @returns 인코딩된 쿼리
141
+ */
142
+ export function makeItemQuery(query: string): string {
143
+ return encodeURI(query);
144
+ }
145
+
146
+ // 기존 호환성을 위한 default export
147
+ export default { UriBuilder, QueryBuilder, Request, makeItemQuery };