@nagiyu/common 0.1.0 → 0.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.
- package/dist/cjs/api/client.js +175 -0
- package/dist/cjs/api/client.js.map +1 -0
- package/dist/cjs/api/error-handler.js +136 -0
- package/dist/cjs/api/error-handler.js.map +1 -0
- package/dist/cjs/api/error-utils.js +21 -0
- package/dist/cjs/api/error-utils.js.map +1 -0
- package/dist/cjs/api/index.js +35 -0
- package/dist/cjs/api/index.js.map +1 -0
- package/dist/cjs/api/types.js +21 -0
- package/dist/cjs/api/types.js.map +1 -0
- package/dist/cjs/async/sleep.js +11 -0
- package/dist/cjs/async/sleep.js.map +1 -0
- package/dist/cjs/auth/auth-error.js +21 -0
- package/dist/cjs/auth/auth-error.js.map +1 -0
- package/dist/cjs/auth/index.js +28 -0
- package/dist/cjs/auth/index.js.map +1 -0
- package/dist/cjs/auth/permissions.js +106 -0
- package/dist/cjs/auth/permissions.js.map +1 -0
- package/dist/cjs/auth/redirect.js +51 -0
- package/dist/cjs/auth/redirect.js.map +1 -0
- package/dist/cjs/auth/role-grants.js +59 -0
- package/dist/cjs/auth/role-grants.js.map +1 -0
- package/dist/cjs/auth/roles.js +204 -0
- package/dist/cjs/auth/roles.js.map +1 -0
- package/dist/cjs/auth/types.js +9 -0
- package/dist/cjs/auth/types.js.map +1 -0
- package/dist/cjs/concurrency/error-messages.js +11 -0
- package/dist/cjs/concurrency/error-messages.js.map +1 -0
- package/dist/cjs/concurrency/run-settled-with-concurrency.js +65 -0
- package/dist/cjs/concurrency/run-settled-with-concurrency.js.map +1 -0
- package/dist/cjs/concurrency/run-with-concurrency.js +45 -0
- package/dist/cjs/concurrency/run-with-concurrency.js.map +1 -0
- package/dist/cjs/constants/error-codes.js +20 -0
- package/dist/cjs/constants/error-codes.js.map +1 -0
- package/dist/cjs/constants/error-messages.js +38 -0
- package/dist/cjs/constants/error-messages.js.map +1 -0
- package/dist/cjs/constants/http-status.js +18 -0
- package/dist/cjs/constants/http-status.js.map +1 -0
- package/dist/cjs/error-event/error-context.js +21 -0
- package/dist/cjs/error-event/error-context.js.map +1 -0
- package/dist/cjs/error-event/event-id.js +22 -0
- package/dist/cjs/error-event/event-id.js.map +1 -0
- package/dist/cjs/error-event/index.js +18 -0
- package/dist/cjs/error-event/index.js.map +1 -0
- package/dist/cjs/error-event/types.js +36 -0
- package/dist/cjs/error-event/types.js.map +1 -0
- package/dist/cjs/format/file-size.js +14 -0
- package/dist/cjs/format/file-size.js.map +1 -0
- package/dist/cjs/format/index.js +28 -0
- package/dist/cjs/format/index.js.map +1 -0
- package/dist/cjs/format/jst-period.js +83 -0
- package/dist/cjs/format/jst-period.js.map +1 -0
- package/dist/cjs/format/jst.js +66 -0
- package/dist/cjs/format/jst.js.map +1 -0
- package/dist/cjs/format/price.js +14 -0
- package/dist/cjs/format/price.js.map +1 -0
- package/dist/cjs/format/timestamp.js +27 -0
- package/dist/cjs/format/timestamp.js.map +1 -0
- package/dist/cjs/index.js +62 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/llm-usage/index.js +10 -0
- package/dist/cjs/llm-usage/index.js.map +1 -0
- package/dist/cjs/llm-usage/types.js +11 -0
- package/dist/cjs/llm-usage/types.js.map +1 -0
- package/dist/cjs/llm-usage/usage-log.js +135 -0
- package/dist/cjs/llm-usage/usage-log.js.map +1 -0
- package/dist/cjs/logger/index.js +13 -0
- package/dist/cjs/logger/index.js.map +1 -0
- package/dist/cjs/logger/logger.js +126 -0
- package/dist/cjs/logger/logger.js.map +1 -0
- package/dist/cjs/logger/types.js +3 -0
- package/dist/cjs/logger/types.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/push/bulk.js +28 -0
- package/dist/cjs/push/bulk.js.map +1 -0
- package/dist/cjs/push/client.js +108 -0
- package/dist/cjs/push/client.js.map +1 -0
- package/dist/cjs/push/config.js +20 -0
- package/dist/cjs/push/config.js.map +1 -0
- package/dist/cjs/push/constants.js +5 -0
- package/dist/cjs/push/constants.js.map +1 -0
- package/dist/cjs/push/index.js +14 -0
- package/dist/cjs/push/index.js.map +1 -0
- package/dist/cjs/push/types.js +3 -0
- package/dist/cjs/push/types.js.map +1 -0
- package/dist/cjs/push/vapid.js +39 -0
- package/dist/cjs/push/vapid.js.map +1 -0
- package/dist/cjs/push-subscription/endpoint.js +48 -0
- package/dist/cjs/push-subscription/endpoint.js.map +1 -0
- package/dist/cjs/push-subscription/subscription-id.js +20 -0
- package/dist/cjs/push-subscription/subscription-id.js.map +1 -0
- package/dist/cjs/retry/index.js +9 -0
- package/dist/cjs/retry/index.js.map +1 -0
- package/dist/cjs/retry/retry.js +52 -0
- package/dist/cjs/retry/retry.js.map +1 -0
- package/dist/cjs/retry/timeout.js +26 -0
- package/dist/cjs/retry/timeout.js.map +1 -0
- package/dist/cjs/retry/types.js +3 -0
- package/dist/cjs/retry/types.js.map +1 -0
- package/dist/cjs/validation/helpers.js +58 -0
- package/dist/cjs/validation/helpers.js.map +1 -0
- package/dist/cjs/validation/index.js +17 -0
- package/dist/cjs/validation/index.js.map +1 -0
- package/dist/cjs/validation/invalid-input-error.js +18 -0
- package/dist/cjs/validation/invalid-input-error.js.map +1 -0
- package/dist/cjs/validation/require-env.js +30 -0
- package/dist/cjs/validation/require-env.js.map +1 -0
- package/dist/cjs/validation/types.js +3 -0
- package/dist/cjs/validation/types.js.map +1 -0
- package/package.json +9 -6
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* API クライアント
|
|
4
|
+
*
|
|
5
|
+
* リトライ機能とエラーハンドリングを統合したFetchラッパー
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.sleep = void 0;
|
|
9
|
+
exports.calculateBackoffDelay = calculateBackoffDelay;
|
|
10
|
+
exports.fetchWithTimeout = fetchWithTimeout;
|
|
11
|
+
exports.apiRequest = apiRequest;
|
|
12
|
+
exports.get = get;
|
|
13
|
+
exports.post = post;
|
|
14
|
+
exports.put = put;
|
|
15
|
+
exports.del = del;
|
|
16
|
+
const error_handler_js_1 = require("./error-handler.js");
|
|
17
|
+
const types_js_1 = require("./types.js");
|
|
18
|
+
const sleep_js_1 = require("../async/sleep.js");
|
|
19
|
+
Object.defineProperty(exports, "sleep", { enumerable: true, get: function () { return sleep_js_1.sleep; } });
|
|
20
|
+
/**
|
|
21
|
+
* デフォルトリトライ設定
|
|
22
|
+
*/
|
|
23
|
+
const DEFAULT_RETRY_CONFIG = {
|
|
24
|
+
maxRetries: 3,
|
|
25
|
+
initialDelay: 1000, // 1秒
|
|
26
|
+
maxDelay: 10000, // 10秒
|
|
27
|
+
backoffMultiplier: 2,
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* デフォルトタイムアウト設定
|
|
31
|
+
*/
|
|
32
|
+
const DEFAULT_TIMEOUT = 30000; // 30秒
|
|
33
|
+
/**
|
|
34
|
+
* エクスポネンシャルバックオフの遅延時間を計算
|
|
35
|
+
*/
|
|
36
|
+
function calculateBackoffDelay(attempt, config) {
|
|
37
|
+
const delay = Math.min(config.initialDelay * Math.pow(config.backoffMultiplier, attempt), config.maxDelay);
|
|
38
|
+
// ジッター追加(遅延時間の±25%のランダム値)
|
|
39
|
+
const jitter = delay * 0.25 * (Math.random() * 2 - 1);
|
|
40
|
+
return Math.floor(delay + jitter);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* タイムアウト付きfetch
|
|
44
|
+
*/
|
|
45
|
+
async function fetchWithTimeout(url, options) {
|
|
46
|
+
const { timeout = DEFAULT_TIMEOUT, ...fetchOptions } = options;
|
|
47
|
+
const controller = new AbortController();
|
|
48
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
49
|
+
try {
|
|
50
|
+
const response = await fetch(url, {
|
|
51
|
+
...fetchOptions,
|
|
52
|
+
signal: controller.signal,
|
|
53
|
+
});
|
|
54
|
+
return response;
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
clearTimeout(timeoutId);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* リトライ機能付きAPIリクエスト
|
|
62
|
+
*
|
|
63
|
+
* @template T - レスポンスデータの型(明示的に指定する必要があります)
|
|
64
|
+
* @param url - リクエストURL
|
|
65
|
+
* @param options - リクエストオプション
|
|
66
|
+
* @returns レスポンスデータ
|
|
67
|
+
*/
|
|
68
|
+
async function apiRequest(url, options = {}) {
|
|
69
|
+
const { retry, timeout, ...fetchOptions } = options;
|
|
70
|
+
const retryConfig = {
|
|
71
|
+
...DEFAULT_RETRY_CONFIG,
|
|
72
|
+
...retry,
|
|
73
|
+
};
|
|
74
|
+
let lastError = null;
|
|
75
|
+
for (let attempt = 0; attempt <= retryConfig.maxRetries; attempt++) {
|
|
76
|
+
try {
|
|
77
|
+
const response = await fetchWithTimeout(url, {
|
|
78
|
+
...fetchOptions,
|
|
79
|
+
timeout,
|
|
80
|
+
});
|
|
81
|
+
// レスポンスが成功の場合
|
|
82
|
+
if (response.ok) {
|
|
83
|
+
// JSONレスポンスをパース
|
|
84
|
+
const data = await response.json();
|
|
85
|
+
return data;
|
|
86
|
+
}
|
|
87
|
+
// レスポンスエラーの場合
|
|
88
|
+
const errorInfo = await (0, error_handler_js_1.extractErrorInfo)(response);
|
|
89
|
+
// リトライ可能なエラーで、まだリトライ回数が残っている場合
|
|
90
|
+
if (errorInfo.shouldRetry && attempt < retryConfig.maxRetries) {
|
|
91
|
+
const delay = calculateBackoffDelay(attempt, retryConfig);
|
|
92
|
+
console.log(`Retrying request to ${url} after ${delay}ms (attempt ${attempt + 1}/${retryConfig.maxRetries})`);
|
|
93
|
+
await (0, sleep_js_1.sleep)(delay);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
// リトライしない、または最終試行の場合はエラーをスロー
|
|
97
|
+
throw new types_js_1.APIError(response.status, errorInfo, errorInfo.message);
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
// fetchレベルのエラー(ネットワークエラー、タイムアウトなど)
|
|
101
|
+
if (error instanceof types_js_1.APIError) {
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
lastError = error;
|
|
105
|
+
const errorInfo = (0, error_handler_js_1.handleFetchError)(error);
|
|
106
|
+
// リトライ可能なエラーで、まだリトライ回数が残っている場合
|
|
107
|
+
if (errorInfo.shouldRetry && attempt < retryConfig.maxRetries) {
|
|
108
|
+
const delay = calculateBackoffDelay(attempt, retryConfig);
|
|
109
|
+
console.log(`Retrying request to ${url} after ${delay}ms (attempt ${attempt + 1}/${retryConfig.maxRetries})`);
|
|
110
|
+
await (0, sleep_js_1.sleep)(delay);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
// リトライしない、または最終試行の場合はエラーをスロー
|
|
114
|
+
throw new types_js_1.APIError(0, errorInfo, errorInfo.message);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
// このコードは理論上到達しないが、TypeScriptの型安全性のために存在
|
|
118
|
+
// すべてのリトライが完了した場合、ループ内でエラーがスローされる
|
|
119
|
+
throw lastError || new Error('Request failed after all retries');
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* GETリクエスト
|
|
123
|
+
*
|
|
124
|
+
* @template T - レスポンスデータの型(明示的に指定する必要があります)
|
|
125
|
+
*/
|
|
126
|
+
async function get(url, options = {}) {
|
|
127
|
+
return apiRequest(url, {
|
|
128
|
+
...options,
|
|
129
|
+
method: 'GET',
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* POSTリクエスト
|
|
134
|
+
*
|
|
135
|
+
* @template T - レスポンスデータの型(明示的に指定する必要があります)
|
|
136
|
+
*/
|
|
137
|
+
async function post(url, body, options = {}) {
|
|
138
|
+
return apiRequest(url, {
|
|
139
|
+
...options,
|
|
140
|
+
method: 'POST',
|
|
141
|
+
headers: {
|
|
142
|
+
'Content-Type': 'application/json',
|
|
143
|
+
...options.headers,
|
|
144
|
+
},
|
|
145
|
+
body: JSON.stringify(body),
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* PUTリクエスト
|
|
150
|
+
*
|
|
151
|
+
* @template T - レスポンスデータの型(明示的に指定する必要があります)
|
|
152
|
+
*/
|
|
153
|
+
async function put(url, body, options = {}) {
|
|
154
|
+
return apiRequest(url, {
|
|
155
|
+
...options,
|
|
156
|
+
method: 'PUT',
|
|
157
|
+
headers: {
|
|
158
|
+
'Content-Type': 'application/json',
|
|
159
|
+
...options.headers,
|
|
160
|
+
},
|
|
161
|
+
body: JSON.stringify(body),
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* DELETEリクエスト
|
|
166
|
+
*
|
|
167
|
+
* @template T - レスポンスデータの型(明示的に指定する必要があります)
|
|
168
|
+
*/
|
|
169
|
+
async function del(url, options = {}) {
|
|
170
|
+
return apiRequest(url, {
|
|
171
|
+
...options,
|
|
172
|
+
method: 'DELETE',
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/api/client.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AA2BH,sDAQC;AAKD,4CAkBC;AAUD,gCAiEC;AAOD,kBAQC;AAOD,oBAcC;AAOD,kBAcC;AAOD,kBAQC;AA3MD,yDAAwE;AACxE,yCAAsC;AAEtC,gDAA0C;AAEjC,sFAFA,gBAAK,OAEA;AAEd;;GAEG;AACH,MAAM,oBAAoB,GAAgB;IACxC,UAAU,EAAE,CAAC;IACb,YAAY,EAAE,IAAI,EAAE,KAAK;IACzB,QAAQ,EAAE,KAAK,EAAE,MAAM;IACvB,iBAAiB,EAAE,CAAC;CACrB,CAAC;AAEF;;GAEG;AACH,MAAM,eAAe,GAA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OAAO,EAAE;YACP,cAAc,EAAE,kBAAkB;YAClC,GAAG,OAAO,CAAC,OAAO;SACnB;QACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;KAC3B,CAAC,CAAC;AACL,CAAC;AAED;;;;GAIG;AACI,KAAK,UAAU,GAAG,CACvB,GAAW,EACX,UAAsD,EAAE;IAExD,OAAO,UAAU,CAAI,GAAG,EAAE;QACxB,GAAG,OAAO;QACV,MAAM,EAAE,QAAQ;KACjB,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* エラーハンドリングユーティリティ
|
|
4
|
+
*
|
|
5
|
+
* APIエラーを統一的に処理し、ユーザーフレンドリーなメッセージに変換する
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.getErrorTypeFromStatus = getErrorTypeFromStatus;
|
|
9
|
+
exports.parseErrorResponse = parseErrorResponse;
|
|
10
|
+
exports.handleFetchError = handleFetchError;
|
|
11
|
+
exports.mapAPIErrorToMessage = mapAPIErrorToMessage;
|
|
12
|
+
exports.isRetryableError = isRetryableError;
|
|
13
|
+
exports.extractErrorInfo = extractErrorInfo;
|
|
14
|
+
const error_messages_js_1 = require("../constants/error-messages.js");
|
|
15
|
+
/**
|
|
16
|
+
* HTTPステータスコードからエラータイプを判定
|
|
17
|
+
*/
|
|
18
|
+
function getErrorTypeFromStatus(status) {
|
|
19
|
+
if (status >= 500) {
|
|
20
|
+
return 'error';
|
|
21
|
+
}
|
|
22
|
+
if (status >= 400) {
|
|
23
|
+
return 'warning';
|
|
24
|
+
}
|
|
25
|
+
return 'info';
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* エラーレスポンスをパース
|
|
29
|
+
*/
|
|
30
|
+
async function parseErrorResponse(response) {
|
|
31
|
+
try {
|
|
32
|
+
const data = await response.json();
|
|
33
|
+
// Type guard to check if data has error and message properties
|
|
34
|
+
if (data &&
|
|
35
|
+
typeof data === 'object' &&
|
|
36
|
+
'error' in data &&
|
|
37
|
+
'message' in data &&
|
|
38
|
+
typeof data.error === 'string' &&
|
|
39
|
+
typeof data.message === 'string') {
|
|
40
|
+
return data;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
// JSONパースエラーは無視
|
|
45
|
+
}
|
|
46
|
+
// レスポンスボディがない場合はステータスコードベースのメッセージを返す
|
|
47
|
+
return {
|
|
48
|
+
error: 'HTTP_ERROR',
|
|
49
|
+
message: `HTTPエラー: ${response.status}`,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Fetch APIのエラーをユーザーフレンドリーなメッセージに変換
|
|
54
|
+
*/
|
|
55
|
+
function handleFetchError(error) {
|
|
56
|
+
// ネットワークエラー (fetch が失敗した場合)
|
|
57
|
+
if (error instanceof TypeError && error.message.includes('fetch')) {
|
|
58
|
+
return {
|
|
59
|
+
type: 'error',
|
|
60
|
+
message: error_messages_js_1.COMMON_ERROR_MESSAGES.NETWORK_ERROR,
|
|
61
|
+
shouldRetry: true,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
// タイムアウトエラー
|
|
65
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
|
66
|
+
return {
|
|
67
|
+
type: 'error',
|
|
68
|
+
message: error_messages_js_1.COMMON_ERROR_MESSAGES.TIMEOUT_ERROR,
|
|
69
|
+
shouldRetry: true,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
// その他のエラー
|
|
73
|
+
return {
|
|
74
|
+
type: 'error',
|
|
75
|
+
message: error_messages_js_1.COMMON_ERROR_MESSAGES.UNKNOWN_ERROR,
|
|
76
|
+
shouldRetry: false,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* APIエラーレスポンスをユーザーフレンドリーなメッセージに変換
|
|
81
|
+
* 2段階マッピング: サービス固有メッセージ → 共通メッセージ → APIレスポンスメッセージ
|
|
82
|
+
*
|
|
83
|
+
* @param errorResponse APIエラーレスポンス
|
|
84
|
+
* @param serviceMessages サービス固有のエラーメッセージマッピング(オプション)
|
|
85
|
+
* @returns ユーザーフレンドリーなエラーメッセージ
|
|
86
|
+
*/
|
|
87
|
+
function mapAPIErrorToMessage(errorResponse, serviceMessages) {
|
|
88
|
+
const errorCode = errorResponse.error;
|
|
89
|
+
// 第1優先: サービス固有メッセージ
|
|
90
|
+
if (serviceMessages && errorCode in serviceMessages) {
|
|
91
|
+
return serviceMessages[errorCode];
|
|
92
|
+
}
|
|
93
|
+
// 第2優先: 共通メッセージマッピング
|
|
94
|
+
const commonMessageMap = {
|
|
95
|
+
UNAUTHORIZED: error_messages_js_1.COMMON_ERROR_MESSAGES.UNAUTHORIZED,
|
|
96
|
+
FORBIDDEN: error_messages_js_1.COMMON_ERROR_MESSAGES.FORBIDDEN,
|
|
97
|
+
INVALID_REQUEST: error_messages_js_1.COMMON_ERROR_MESSAGES.INVALID_REQUEST,
|
|
98
|
+
VALIDATION_ERROR: error_messages_js_1.COMMON_ERROR_MESSAGES.VALIDATION_ERROR,
|
|
99
|
+
NOT_FOUND: error_messages_js_1.COMMON_ERROR_MESSAGES.NOT_FOUND,
|
|
100
|
+
INTERNAL_ERROR: error_messages_js_1.COMMON_ERROR_MESSAGES.SERVER_ERROR,
|
|
101
|
+
};
|
|
102
|
+
if (errorCode in commonMessageMap) {
|
|
103
|
+
return commonMessageMap[errorCode];
|
|
104
|
+
}
|
|
105
|
+
// 第3優先: APIレスポンスのメッセージをそのまま使用
|
|
106
|
+
return errorResponse.message || error_messages_js_1.COMMON_ERROR_MESSAGES.UNKNOWN_ERROR;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* エラーがリトライ可能か判定
|
|
110
|
+
*/
|
|
111
|
+
function isRetryableError(status) {
|
|
112
|
+
// ネットワークエラー、タイムアウト、サーバーエラーはリトライ可能
|
|
113
|
+
if (status === 0 || status === 408 || status >= 500) {
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
// 429 (Too Many Requests) もリトライ可能
|
|
117
|
+
if (status === 429) {
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Responseからエラー情報を抽出
|
|
124
|
+
*/
|
|
125
|
+
async function extractErrorInfo(response) {
|
|
126
|
+
const errorResponse = await parseErrorResponse(response);
|
|
127
|
+
const message = mapAPIErrorToMessage(errorResponse);
|
|
128
|
+
const shouldRetry = isRetryableError(response.status);
|
|
129
|
+
return {
|
|
130
|
+
type: getErrorTypeFromStatus(response.status),
|
|
131
|
+
message,
|
|
132
|
+
details: errorResponse.details,
|
|
133
|
+
shouldRetry,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=error-handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-handler.js","sourceRoot":"","sources":["../../../src/api/error-handler.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;AAQH,wDAQC;AAKD,gDAuBC;AAKD,4CAyBC;AAUD,oDA2BC;AAKD,4CAYC;AAKD,4CAWC;AA7ID,sEAAuE;AAEvE;;GAEG;AACH,SAAgB,sBAAsB,CAAC,MAAc;IACnD,IAAI,MAAM,IAAI,GAAG,EAAE,CAAC;QAClB,OAAO,OAAO,CAAC;IACjB,CAAC;IACD,IAAI,MAAM,IAAI,GAAG,EAAE,CAAC;QAClB,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;GAEG;AACI,KAAK,UAAU,kBAAkB,CAAC,QAAkB;IACzD,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QACnC,+DAA+D;QAC/D,IACE,IAAI;YACJ,OAAO,IAAI,KAAK,QAAQ;YACx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Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toErrorMessage = toErrorMessage;
|
|
4
|
+
exports.extractErrorMessage = extractErrorMessage;
|
|
5
|
+
function toErrorMessage(error) {
|
|
6
|
+
return error instanceof Error ? error.message : String(error);
|
|
7
|
+
}
|
|
8
|
+
function extractErrorMessage(errorData, defaultMessage) {
|
|
9
|
+
if (typeof errorData !== 'object' || errorData === null) {
|
|
10
|
+
return defaultMessage;
|
|
11
|
+
}
|
|
12
|
+
const candidate = errorData;
|
|
13
|
+
if (typeof candidate.message === 'string') {
|
|
14
|
+
return candidate.message;
|
|
15
|
+
}
|
|
16
|
+
if (typeof candidate.error?.message === 'string') {
|
|
17
|
+
return candidate.error.message;
|
|
18
|
+
}
|
|
19
|
+
return defaultMessage;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=error-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-utils.js","sourceRoot":"","sources":["../../../src/api/error-utils.ts"],"names":[],"mappings":";;AAAA,wCAEC;AAED,kDAqBC;AAzBD,SAAgB,cAAc,CAAC,KAAc;IAC3C,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAChE,CAAC;AAED,SAAgB,mBAAmB,CAAC,SAAkB,EAAE,cAAsB;IAC5E,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,SAAS,KAAK,IAAI,EAAE,CAAC;QACxD,OAAO,cAAc,CAAC;IACxB,CAAC;IAED,MAAM,SAAS,GAAG,SAKjB,CAAC;IAEF,IAAI,OAAO,SAAS,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;QAC1C,OAAO,SAAS,CAAC,OAAO,CAAC;IAC3B,CAAC;IAED,IAAI,OAAO,SAAS,CAAC,KAAK,EAAE,OAAO,KAAK,QAAQ,EAAE,CAAC;QACjD,OAAO,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC;IACjC,CAAC;IAED,OAAO,cAAc,CAAC;AACxB,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* API module
|
|
4
|
+
*
|
|
5
|
+
* Provides common API-related utilities and types for HTTP communication.
|
|
6
|
+
* This module is framework-agnostic and can be used across all services.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.del = exports.put = exports.post = exports.get = exports.apiRequest = exports.fetchWithTimeout = exports.sleep = exports.calculateBackoffDelay = exports.toErrorMessage = exports.extractErrorMessage = exports.extractErrorInfo = exports.isRetryableError = exports.mapAPIErrorToMessage = exports.handleFetchError = exports.parseErrorResponse = exports.getErrorTypeFromStatus = exports.COMMON_ERROR_MESSAGES = exports.APIError = void 0;
|
|
10
|
+
var types_js_1 = require("./types.js");
|
|
11
|
+
Object.defineProperty(exports, "APIError", { enumerable: true, get: function () { return types_js_1.APIError; } });
|
|
12
|
+
var error_messages_js_1 = require("../constants/error-messages.js");
|
|
13
|
+
Object.defineProperty(exports, "COMMON_ERROR_MESSAGES", { enumerable: true, get: function () { return error_messages_js_1.COMMON_ERROR_MESSAGES; } });
|
|
14
|
+
// Export error handling utilities
|
|
15
|
+
var error_handler_js_1 = require("./error-handler.js");
|
|
16
|
+
Object.defineProperty(exports, "getErrorTypeFromStatus", { enumerable: true, get: function () { return error_handler_js_1.getErrorTypeFromStatus; } });
|
|
17
|
+
Object.defineProperty(exports, "parseErrorResponse", { enumerable: true, get: function () { return error_handler_js_1.parseErrorResponse; } });
|
|
18
|
+
Object.defineProperty(exports, "handleFetchError", { enumerable: true, get: function () { return error_handler_js_1.handleFetchError; } });
|
|
19
|
+
Object.defineProperty(exports, "mapAPIErrorToMessage", { enumerable: true, get: function () { return error_handler_js_1.mapAPIErrorToMessage; } });
|
|
20
|
+
Object.defineProperty(exports, "isRetryableError", { enumerable: true, get: function () { return error_handler_js_1.isRetryableError; } });
|
|
21
|
+
Object.defineProperty(exports, "extractErrorInfo", { enumerable: true, get: function () { return error_handler_js_1.extractErrorInfo; } });
|
|
22
|
+
var error_utils_js_1 = require("./error-utils.js");
|
|
23
|
+
Object.defineProperty(exports, "extractErrorMessage", { enumerable: true, get: function () { return error_utils_js_1.extractErrorMessage; } });
|
|
24
|
+
Object.defineProperty(exports, "toErrorMessage", { enumerable: true, get: function () { return error_utils_js_1.toErrorMessage; } });
|
|
25
|
+
// Export API client
|
|
26
|
+
var client_js_1 = require("./client.js");
|
|
27
|
+
Object.defineProperty(exports, "calculateBackoffDelay", { enumerable: true, get: function () { return client_js_1.calculateBackoffDelay; } });
|
|
28
|
+
Object.defineProperty(exports, "sleep", { enumerable: true, get: function () { return client_js_1.sleep; } });
|
|
29
|
+
Object.defineProperty(exports, "fetchWithTimeout", { enumerable: true, get: function () { return client_js_1.fetchWithTimeout; } });
|
|
30
|
+
Object.defineProperty(exports, "apiRequest", { enumerable: true, get: function () { return client_js_1.apiRequest; } });
|
|
31
|
+
Object.defineProperty(exports, "get", { enumerable: true, get: function () { return client_js_1.get; } });
|
|
32
|
+
Object.defineProperty(exports, "post", { enumerable: true, get: function () { return client_js_1.post; } });
|
|
33
|
+
Object.defineProperty(exports, "put", { enumerable: true, get: function () { return client_js_1.put; } });
|
|
34
|
+
Object.defineProperty(exports, "del", { enumerable: true, get: function () { return client_js_1.del; } });
|
|
35
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/api/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAcH,uCAAsC;AAA7B,oGAAA,QAAQ,OAAA;AACjB,oEAAuE;AAA9D,0HAAA,qBAAqB,OAAA;AAE9B,kCAAkC;AAClC,uDAO4B;AAN1B,0HAAA,sBAAsB,OAAA;AACtB,sHAAA,kBAAkB,OAAA;AAClB,oHAAA,gBAAgB,OAAA;AAChB,wHAAA,oBAAoB,OAAA;AACpB,oHAAA,gBAAgB,OAAA;AAChB,oHAAA,gBAAgB,OAAA;AAElB,mDAAuE;AAA9D,qHAAA,mBAAmB,OAAA;AAAE,gHAAA,cAAc,OAAA;AAE5C,oBAAoB;AACpB,yCASqB;AARnB,kHAAA,qBAAqB,OAAA;AACrB,kGAAA,KAAK,OAAA;AACL,6GAAA,gBAAgB,OAAA;AAChB,uGAAA,UAAU,OAAA;AACV,gGAAA,GAAG,OAAA;AACH,iGAAA,IAAI,OAAA;AACJ,gGAAA,GAAG,OAAA;AACH,gGAAA,GAAG,OAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* API Common Types
|
|
4
|
+
*
|
|
5
|
+
* Framework-agnostic type definitions for API communication.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.APIError = void 0;
|
|
9
|
+
/**
|
|
10
|
+
* APIエラー
|
|
11
|
+
*/
|
|
12
|
+
class APIError extends Error {
|
|
13
|
+
constructor(status, errorInfo, message) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.status = status;
|
|
16
|
+
this.errorInfo = errorInfo;
|
|
17
|
+
this.name = 'APIError';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.APIError = APIError;
|
|
21
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/api/types.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAyEH;;GAEG;AACH,MAAa,QAAS,SAAQ,KAAK;IAIjC,YAAY,MAAc,EAAE,SAAoB,EAAE,OAAe;QAC/D,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;IACzB,CAAC;CACF;AAVD,4BAUC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sleep = sleep;
|
|
4
|
+
/**
|
|
5
|
+
* 指定ミリ秒だけ待機する。
|
|
6
|
+
* リトライ間隔などの待機を各所で重複実装しないよう、共通の実装をここに置く。
|
|
7
|
+
*/
|
|
8
|
+
function sleep(ms) {
|
|
9
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=sleep.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sleep.js","sourceRoot":"","sources":["../../../src/async/sleep.ts"],"names":[],"mappings":";;AAIA,sBAEC;AAND;;;GAGG;AACH,SAAgB,KAAK,CAAC,EAAU;IAC9B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAC3D,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAuthError = getAuthError;
|
|
4
|
+
const permissions_js_1 = require("./permissions.js");
|
|
5
|
+
const error_messages_js_1 = require("../constants/error-messages.js");
|
|
6
|
+
function getAuthError(session, permission) {
|
|
7
|
+
if (!session) {
|
|
8
|
+
return {
|
|
9
|
+
message: error_messages_js_1.COMMON_ERROR_MESSAGES.UNAUTHORIZED,
|
|
10
|
+
statusCode: 401,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
if (!(0, permissions_js_1.hasPermission)(session.user.roles, permission)) {
|
|
14
|
+
return {
|
|
15
|
+
message: error_messages_js_1.COMMON_ERROR_MESSAGES.FORBIDDEN,
|
|
16
|
+
statusCode: 403,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=auth-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-error.js","sourceRoot":"","sources":["../../../src/auth/auth-error.ts"],"names":[],"mappings":";;AAIA,oCAmBC;AAvBD,qDAAiD;AACjD,sEAAuE;AAGvE,SAAgB,YAAY,CAC1B,OAAuB,EACvB,UAAsB;IAEtB,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO;YACL,OAAO,EAAE,yCAAqB,CAAC,YAAY;YAC3C,UAAU,EAAE,GAAG;SAChB,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,IAAA,8BAAa,EAAC,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,UAAU,CAAC,EAAE,CAAC;QACnD,OAAO;YACL,OAAO,EAAE,yCAAqB,CAAC,SAAS;YACxC,UAAU,EAAE,GAAG;SAChB,CAAC;IACJ,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Auth Module
|
|
4
|
+
*
|
|
5
|
+
* Exports all authentication and authorization related types, constants, and functions.
|
|
6
|
+
*/
|
|
7
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
8
|
+
if (k2 === undefined) k2 = k;
|
|
9
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
10
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
+
}
|
|
13
|
+
Object.defineProperty(o, k2, desc);
|
|
14
|
+
}) : (function(o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
o[k2] = m[k];
|
|
17
|
+
}));
|
|
18
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
19
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
__exportStar(require("./types.js"), exports);
|
|
23
|
+
__exportStar(require("./roles.js"), exports);
|
|
24
|
+
__exportStar(require("./role-grants.js"), exports);
|
|
25
|
+
__exportStar(require("./permissions.js"), exports);
|
|
26
|
+
__exportStar(require("./auth-error.js"), exports);
|
|
27
|
+
__exportStar(require("./redirect.js"), exports);
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/auth/index.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;;;;;;;;;;;;;;AAEH,6CAA2B;AAC3B,6CAA2B;AAC3B,mDAAiC;AACjC,mDAAiC;AACjC,kDAAgC;AAChC,gDAA8B"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Permission Checking Functions
|
|
4
|
+
*
|
|
5
|
+
* Provides utility functions for checking user permissions based on their assigned roles.
|
|
6
|
+
* These functions implement role-based access control (RBAC) for the nagiyu platform.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.hasPermission = hasPermission;
|
|
10
|
+
exports.hasUserPermission = hasUserPermission;
|
|
11
|
+
exports.requirePermission = requirePermission;
|
|
12
|
+
exports.hasAnyPermission = hasAnyPermission;
|
|
13
|
+
exports.hasAllPermissions = hasAllPermissions;
|
|
14
|
+
const roles_js_1 = require("./roles.js");
|
|
15
|
+
/**
|
|
16
|
+
* Check if the user has a specific permission
|
|
17
|
+
*
|
|
18
|
+
* @param roles - Array of role IDs assigned to the user
|
|
19
|
+
* @param permission - Required permission in the format {resource}:{action}
|
|
20
|
+
* @returns true if the user has the permission, false otherwise
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* const canRead = hasPermission(['user-manager'], 'users:read'); // true
|
|
25
|
+
* const canAssign = hasPermission(['user-manager'], 'roles:assign'); // false
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
function hasPermission(roles, permission) {
|
|
29
|
+
// Flatten all permissions from all assigned roles
|
|
30
|
+
const userPermissions = roles.flatMap((roleId) => roles_js_1.ROLES[roleId]?.permissions ?? []);
|
|
31
|
+
return userPermissions.includes(permission);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Check if a session user has a specific permission
|
|
35
|
+
*
|
|
36
|
+
* session.user.roles は next-auth の型拡張に依存せず、未ログイン・roles 未設定・
|
|
37
|
+
* 想定外の型のいずれでも例外にせず false を返したいので、unknown として受けて検証する。
|
|
38
|
+
*
|
|
39
|
+
* @param user - Session user (null/undefined when not signed in)
|
|
40
|
+
* @param permission - Required permission in the format {resource}:{action}
|
|
41
|
+
* @returns true if the user has an array of roles that grants the permission, false otherwise
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const canChat = hasUserPermission(session?.user, 'livetalk:chat');
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
function hasUserPermission(user, permission) {
|
|
49
|
+
if (!user || !Array.isArray(user.roles)) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return hasPermission(user.roles, permission);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Require a specific permission, throwing an error if not granted
|
|
56
|
+
*
|
|
57
|
+
* @param roles - Array of role IDs assigned to the user
|
|
58
|
+
* @param permission - Required permission in the format {resource}:{action}
|
|
59
|
+
* @throws Error if the user does not have the required permission
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* try {
|
|
64
|
+
* requirePermission(['user-manager'], 'roles:assign');
|
|
65
|
+
* } catch (error) {
|
|
66
|
+
* // Handle permission denied
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
function requirePermission(roles, permission) {
|
|
71
|
+
if (!hasPermission(roles, permission)) {
|
|
72
|
+
throw new Error(`Permission denied: ${permission}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Check if the user has any of the specified permissions (OR condition)
|
|
77
|
+
*
|
|
78
|
+
* @param roles - Array of role IDs assigned to the user
|
|
79
|
+
* @param permissions - Array of permissions to check
|
|
80
|
+
* @returns true if the user has at least one of the permissions, false otherwise
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* const canAccess = hasAnyPermission(['user-manager'], ['users:read', 'users:write']); // true
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
function hasAnyPermission(roles, permissions) {
|
|
88
|
+
return permissions.some((permission) => hasPermission(roles, permission));
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Check if the user has all of the specified permissions (AND condition)
|
|
92
|
+
*
|
|
93
|
+
* @param roles - Array of role IDs assigned to the user
|
|
94
|
+
* @param permissions - Array of permissions to check
|
|
95
|
+
* @returns true if the user has all of the permissions, false otherwise
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* const canManage = hasAllPermissions(['admin'], ['users:read', 'users:write']); // true
|
|
100
|
+
* const canManage2 = hasAllPermissions(['user-manager'], ['users:read', 'roles:assign']); // false
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
function hasAllPermissions(roles, permissions) {
|
|
104
|
+
return permissions.every((permission) => hasPermission(roles, permission));
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=permissions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"permissions.js","sourceRoot":"","sources":["../../../src/auth/permissions.ts"],"names":[],"mappings":";AA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,CAAC,aAAa,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC;AAC7E,CAAC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* リダイレクト先 URL の許可判定(オープンリダイレクト対策)
|
|
4
|
+
*
|
|
5
|
+
* 認証フローでは、外部から渡される callbackUrl / リダイレクト URL を
|
|
6
|
+
* 検証せずに遷移先へ用いるとオープンリダイレクトになる。
|
|
7
|
+
* 文字列の前方一致(例: `url.startsWith(baseUrl)` や
|
|
8
|
+
* `/^https?:\/\/[^/]*\.nagiyu\.com/`)は
|
|
9
|
+
* `https://auth.nagiyu.com.evil.com` のようにホスト名の途中に
|
|
10
|
+
* 許可ドメインを含む URL を誤って許可してしまうため使用しない。
|
|
11
|
+
*
|
|
12
|
+
* 本モジュールはフレームワーク非依存・外部依存なしのため、
|
|
13
|
+
* サーバー/クライアント双方から安全に利用できる。
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.isAllowedNagiyuRedirectUrl = isAllowedNagiyuRedirectUrl;
|
|
17
|
+
/**
|
|
18
|
+
* リダイレクト先 URL がプラットフォーム内(同一オリジン、または
|
|
19
|
+
* `nagiyu.com` 本体・そのサブドメイン)かを判定する。
|
|
20
|
+
*
|
|
21
|
+
* - URL としてパースできない(相対パス等)→ false
|
|
22
|
+
* - http / https 以外(javascript:, data: 等)→ false
|
|
23
|
+
* - baseUrl と同一オリジン → true
|
|
24
|
+
* - ホスト名が `nagiyu.com` 完全一致、または `.nagiyu.com` 末尾一致 → true
|
|
25
|
+
*
|
|
26
|
+
* @param rawUrl - 検証対象の URL 文字列
|
|
27
|
+
* @param baseUrl - 自サービスのベース URL(同一オリジン判定に使用)
|
|
28
|
+
* @returns 許可される場合 true
|
|
29
|
+
*/
|
|
30
|
+
function isAllowedNagiyuRedirectUrl(rawUrl, baseUrl) {
|
|
31
|
+
let parsed;
|
|
32
|
+
try {
|
|
33
|
+
parsed = new URL(rawUrl);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
if (parsed.origin === new URL(baseUrl).origin) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// baseUrl 自体が不正な場合はホスト名判定に委ねる
|
|
48
|
+
}
|
|
49
|
+
return parsed.hostname === 'nagiyu.com' || parsed.hostname.endsWith('.nagiyu.com');
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=redirect.js.map
|