@shware/http 2.7.1 → 2.9.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.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/error/parse.ts"],"sourcesContent":["import type { DefaultNamespace, Namespace, TFunction } from 'i18next';\nimport { hasText } from '@shware/utils';\nimport type { ResolvedErrorReason } from './reason';\nimport type { ErrorBody } from './status';\nimport { type BadRequest, DetailType, type ErrorInfo } from './detail';\n\nexport function getErrorInfo(data: unknown): ErrorInfo | null {\n if (typeof data !== 'object' || data === null || !('error' in data)) return null;\n const { error } = data as ErrorBody;\n const errorInfo = error?.details?.find((d) => d['@type'] === DetailType.ERROR_INFO);\n return errorInfo ?? null;\n}\n\n/**\n * @example For axios:\n *\n * const { t } = useTranslation('error');\n *\n * const { message } = getErrorMessage(error.response.data, t);\n * toast.error(message);\n */\nexport function getErrorMessage<Ns extends Namespace = DefaultNamespace, KPrefix = undefined>(\n data: unknown,\n t: TFunction<Ns, KPrefix>\n): { reason: ResolvedErrorReason | null; message: string } {\n // 0. unknown error\n if (typeof data !== 'object' || data === null || !('error' in data)) {\n console.error('Unknown API error:', data);\n return { reason: null, message: t('UNKNOWN') };\n }\n\n const { error } = data as ErrorBody;\n\n // 1. from server via Accept-Language header or lng param\n const localizedMessage = error?.details?.find((d) => d['@type'] === DetailType.LOCALIZED_MESSAGE);\n if (localizedMessage) return { reason: null, message: localizedMessage.message };\n\n // 2. from business logic error\n const errorInfo = error?.details?.find((d) => d['@type'] === DetailType.ERROR_INFO);\n if (errorInfo) {\n return {\n reason: errorInfo.reason,\n message: t(errorInfo.reason, errorInfo.metadata),\n };\n }\n\n // 3. error message in english\n if (hasText(error.message)) return { reason: null, message: error.message };\n\n // 4. from server via status code\n if (error.status) return { reason: null, message: t(error.status) };\n\n // 5. unknown error\n console.error('Unknown API error:', data);\n return { reason: null, message: t('UNKNOWN') };\n}\n\n/**\n * @example For react-hook-form:\n *\n * const { setError } = useForm();\n * const fieldViolations = getFieldViolations(error.response.data);\n * fieldViolations.forEach((violation) => {\n * setError(violation.field, { message: violation.description });\n * });\n */\nexport function getFieldViolations(data: unknown): BadRequest['fieldViolations'] {\n if (typeof data !== 'object' || data === null || !('error' in data)) return [];\n const { error } = data as ErrorBody;\n const badRequest = error?.details?.find((d) => d['@type'] === DetailType.BAD_REQUEST);\n return badRequest?.fieldViolations ?? [];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAAwB;AAGxB,oBAA4D;AAErD,SAAS,aAAa,MAAiC;AAC5D,MAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,MAAO,QAAO;AAC5E,QAAM,EAAE,MAAM,IAAI;AAClB,QAAM,YAAY,OAAO,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,yBAAW,UAAU;AAClF,SAAO,aAAa;AACtB;AAUO,SAAS,gBACd,MACA,GACyD;AAEzD,MAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,OAAO;AACnE,YAAQ,MAAM,sBAAsB,IAAI;AACxC,WAAO,EAAE,QAAQ,MAAM,SAAS,EAAE,SAAS,EAAE;AAAA,EAC/C;AAEA,QAAM,EAAE,MAAM,IAAI;AAGlB,QAAM,mBAAmB,OAAO,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,yBAAW,iBAAiB;AAChG,MAAI,iBAAkB,QAAO,EAAE,QAAQ,MAAM,SAAS,iBAAiB,QAAQ;AAG/E,QAAM,YAAY,OAAO,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,yBAAW,UAAU;AAClF,MAAI,WAAW;AACb,WAAO;AAAA,MACL,QAAQ,UAAU;AAAA,MAClB,SAAS,EAAE,UAAU,QAAQ,UAAU,QAAQ;AAAA,IACjD;AAAA,EACF;AAGA,UAAI,sBAAQ,MAAM,OAAO,EAAG,QAAO,EAAE,QAAQ,MAAM,SAAS,MAAM,QAAQ;AAG1E,MAAI,MAAM,OAAQ,QAAO,EAAE,QAAQ,MAAM,SAAS,EAAE,MAAM,MAAM,EAAE;AAGlE,UAAQ,MAAM,sBAAsB,IAAI;AACxC,SAAO,EAAE,QAAQ,MAAM,SAAS,EAAE,SAAS,EAAE;AAC/C;AAWO,SAAS,mBAAmB,MAA8C;AAC/E,MAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,MAAO,QAAO,CAAC;AAC7E,QAAM,EAAE,MAAM,IAAI;AAClB,QAAM,aAAa,OAAO,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,yBAAW,WAAW;AACpF,SAAO,YAAY,mBAAmB,CAAC;AACzC;","names":[]}
1
+ {"version":3,"sources":["../../src/error/parse.ts"],"sourcesContent":["import { hasText } from '@shware/utils';\nimport type { DefaultNamespace, Namespace, TFunction } from 'i18next';\nimport { type BadRequest, DetailType, type ErrorInfo } from './detail';\nimport type { ResolvedErrorReason } from './reason';\nimport type { ErrorBody } from './status';\n\nexport function getErrorInfo(data: unknown): ErrorInfo | null {\n if (typeof data !== 'object' || data === null || !('error' in data)) return null;\n const { error } = data as ErrorBody;\n const errorInfo = error?.details?.find((d) => d['@type'] === DetailType.ERROR_INFO);\n return errorInfo ?? null;\n}\n\n/**\n * @example For axios:\n *\n * const { t } = useTranslation('error');\n *\n * const { message } = getErrorMessage(error.response.data, t);\n * toast.error(message);\n */\nexport function getErrorMessage<Ns extends Namespace = DefaultNamespace, KPrefix = undefined>(\n data: unknown,\n t: TFunction<Ns, KPrefix>\n): { reason: ResolvedErrorReason | null; message: string } {\n // 0. unknown error\n if (typeof data !== 'object' || data === null || !('error' in data)) {\n console.error('Unknown API error:', data);\n return { reason: null, message: t('UNKNOWN') };\n }\n\n const { error } = data as ErrorBody;\n\n // 1. from server via Accept-Language header or lng param\n const localizedMessage = error?.details?.find((d) => d['@type'] === DetailType.LOCALIZED_MESSAGE);\n if (localizedMessage) return { reason: null, message: localizedMessage.message };\n\n // 2. from business logic error\n const errorInfo = error?.details?.find((d) => d['@type'] === DetailType.ERROR_INFO);\n if (errorInfo) {\n return {\n reason: errorInfo.reason,\n message: t(errorInfo.reason, errorInfo.metadata),\n };\n }\n\n // 3. error message in english\n if (hasText(error.message)) return { reason: null, message: error.message };\n\n // 4. from server via status code\n if (error.status) return { reason: null, message: t(error.status) };\n\n // 5. unknown error\n console.error('Unknown API error:', data);\n return { reason: null, message: t('UNKNOWN') };\n}\n\n/**\n * @example For react-hook-form:\n *\n * const { setError } = useForm();\n * const fieldViolations = getFieldViolations(error.response.data);\n * fieldViolations.forEach((violation) => {\n * setError(violation.field, { message: violation.description });\n * });\n */\nexport function getFieldViolations(data: unknown): BadRequest['fieldViolations'] {\n if (typeof data !== 'object' || data === null || !('error' in data)) return [];\n const { error } = data as ErrorBody;\n const badRequest = error?.details?.find((d) => d['@type'] === DetailType.BAD_REQUEST);\n return badRequest?.fieldViolations ?? [];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAwB;AAExB,oBAA4D;AAIrD,SAAS,aAAa,MAAiC;AAC5D,MAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,MAAO,QAAO;AAC5E,QAAM,EAAE,MAAM,IAAI;AAClB,QAAM,YAAY,OAAO,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,yBAAW,UAAU;AAClF,SAAO,aAAa;AACtB;AAUO,SAAS,gBACd,MACA,GACyD;AAEzD,MAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,OAAO;AACnE,YAAQ,MAAM,sBAAsB,IAAI;AACxC,WAAO,EAAE,QAAQ,MAAM,SAAS,EAAE,SAAS,EAAE;AAAA,EAC/C;AAEA,QAAM,EAAE,MAAM,IAAI;AAGlB,QAAM,mBAAmB,OAAO,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,yBAAW,iBAAiB;AAChG,MAAI,iBAAkB,QAAO,EAAE,QAAQ,MAAM,SAAS,iBAAiB,QAAQ;AAG/E,QAAM,YAAY,OAAO,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,yBAAW,UAAU;AAClF,MAAI,WAAW;AACb,WAAO;AAAA,MACL,QAAQ,UAAU;AAAA,MAClB,SAAS,EAAE,UAAU,QAAQ,UAAU,QAAQ;AAAA,IACjD;AAAA,EACF;AAGA,UAAI,sBAAQ,MAAM,OAAO,EAAG,QAAO,EAAE,QAAQ,MAAM,SAAS,MAAM,QAAQ;AAG1E,MAAI,MAAM,OAAQ,QAAO,EAAE,QAAQ,MAAM,SAAS,EAAE,MAAM,MAAM,EAAE;AAGlE,UAAQ,MAAM,sBAAsB,IAAI;AACxC,SAAO,EAAE,QAAQ,MAAM,SAAS,EAAE,SAAS,EAAE;AAC/C;AAWO,SAAS,mBAAmB,MAA8C;AAC/E,MAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,MAAO,QAAO,CAAC;AAC7E,QAAM,EAAE,MAAM,IAAI;AAClB,QAAM,aAAa,OAAO,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,yBAAW,WAAW;AACpF,SAAO,YAAY,mBAAmB,CAAC;AACzC;","names":[]}
@@ -1,6 +1,6 @@
1
1
  import { Namespace, DefaultNamespace, TFunction } from 'i18next';
2
- import { ResolvedErrorReason } from './reason.cjs';
3
2
  import { ErrorInfo, BadRequest } from './detail.cjs';
3
+ import { ResolvedErrorReason } from './reason.cjs';
4
4
 
5
5
  declare function getErrorInfo(data: unknown): ErrorInfo | null;
6
6
  /**
@@ -1,6 +1,6 @@
1
1
  import { Namespace, DefaultNamespace, TFunction } from 'i18next';
2
- import { ResolvedErrorReason } from './reason.js';
3
2
  import { ErrorInfo, BadRequest } from './detail.js';
3
+ import { ResolvedErrorReason } from './reason.js';
4
4
 
5
5
  declare function getErrorInfo(data: unknown): ErrorInfo | null;
6
6
  /**
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/error/parse.ts"],"sourcesContent":["import type { DefaultNamespace, Namespace, TFunction } from 'i18next';\nimport { hasText } from '@shware/utils';\nimport type { ResolvedErrorReason } from './reason';\nimport type { ErrorBody } from './status';\nimport { type BadRequest, DetailType, type ErrorInfo } from './detail';\n\nexport function getErrorInfo(data: unknown): ErrorInfo | null {\n if (typeof data !== 'object' || data === null || !('error' in data)) return null;\n const { error } = data as ErrorBody;\n const errorInfo = error?.details?.find((d) => d['@type'] === DetailType.ERROR_INFO);\n return errorInfo ?? null;\n}\n\n/**\n * @example For axios:\n *\n * const { t } = useTranslation('error');\n *\n * const { message } = getErrorMessage(error.response.data, t);\n * toast.error(message);\n */\nexport function getErrorMessage<Ns extends Namespace = DefaultNamespace, KPrefix = undefined>(\n data: unknown,\n t: TFunction<Ns, KPrefix>\n): { reason: ResolvedErrorReason | null; message: string } {\n // 0. unknown error\n if (typeof data !== 'object' || data === null || !('error' in data)) {\n console.error('Unknown API error:', data);\n return { reason: null, message: t('UNKNOWN') };\n }\n\n const { error } = data as ErrorBody;\n\n // 1. from server via Accept-Language header or lng param\n const localizedMessage = error?.details?.find((d) => d['@type'] === DetailType.LOCALIZED_MESSAGE);\n if (localizedMessage) return { reason: null, message: localizedMessage.message };\n\n // 2. from business logic error\n const errorInfo = error?.details?.find((d) => d['@type'] === DetailType.ERROR_INFO);\n if (errorInfo) {\n return {\n reason: errorInfo.reason,\n message: t(errorInfo.reason, errorInfo.metadata),\n };\n }\n\n // 3. error message in english\n if (hasText(error.message)) return { reason: null, message: error.message };\n\n // 4. from server via status code\n if (error.status) return { reason: null, message: t(error.status) };\n\n // 5. unknown error\n console.error('Unknown API error:', data);\n return { reason: null, message: t('UNKNOWN') };\n}\n\n/**\n * @example For react-hook-form:\n *\n * const { setError } = useForm();\n * const fieldViolations = getFieldViolations(error.response.data);\n * fieldViolations.forEach((violation) => {\n * setError(violation.field, { message: violation.description });\n * });\n */\nexport function getFieldViolations(data: unknown): BadRequest['fieldViolations'] {\n if (typeof data !== 'object' || data === null || !('error' in data)) return [];\n const { error } = data as ErrorBody;\n const badRequest = error?.details?.find((d) => d['@type'] === DetailType.BAD_REQUEST);\n return badRequest?.fieldViolations ?? [];\n}\n"],"mappings":";AACA,SAAS,eAAe;AAGxB,SAA0B,kBAAkC;AAErD,SAAS,aAAa,MAAiC;AAC5D,MAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,MAAO,QAAO;AAC5E,QAAM,EAAE,MAAM,IAAI;AAClB,QAAM,YAAY,OAAO,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,WAAW,UAAU;AAClF,SAAO,aAAa;AACtB;AAUO,SAAS,gBACd,MACA,GACyD;AAEzD,MAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,OAAO;AACnE,YAAQ,MAAM,sBAAsB,IAAI;AACxC,WAAO,EAAE,QAAQ,MAAM,SAAS,EAAE,SAAS,EAAE;AAAA,EAC/C;AAEA,QAAM,EAAE,MAAM,IAAI;AAGlB,QAAM,mBAAmB,OAAO,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,WAAW,iBAAiB;AAChG,MAAI,iBAAkB,QAAO,EAAE,QAAQ,MAAM,SAAS,iBAAiB,QAAQ;AAG/E,QAAM,YAAY,OAAO,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,WAAW,UAAU;AAClF,MAAI,WAAW;AACb,WAAO;AAAA,MACL,QAAQ,UAAU;AAAA,MAClB,SAAS,EAAE,UAAU,QAAQ,UAAU,QAAQ;AAAA,IACjD;AAAA,EACF;AAGA,MAAI,QAAQ,MAAM,OAAO,EAAG,QAAO,EAAE,QAAQ,MAAM,SAAS,MAAM,QAAQ;AAG1E,MAAI,MAAM,OAAQ,QAAO,EAAE,QAAQ,MAAM,SAAS,EAAE,MAAM,MAAM,EAAE;AAGlE,UAAQ,MAAM,sBAAsB,IAAI;AACxC,SAAO,EAAE,QAAQ,MAAM,SAAS,EAAE,SAAS,EAAE;AAC/C;AAWO,SAAS,mBAAmB,MAA8C;AAC/E,MAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,MAAO,QAAO,CAAC;AAC7E,QAAM,EAAE,MAAM,IAAI;AAClB,QAAM,aAAa,OAAO,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,WAAW,WAAW;AACpF,SAAO,YAAY,mBAAmB,CAAC;AACzC;","names":[]}
1
+ {"version":3,"sources":["../../src/error/parse.ts"],"sourcesContent":["import { hasText } from '@shware/utils';\nimport type { DefaultNamespace, Namespace, TFunction } from 'i18next';\nimport { type BadRequest, DetailType, type ErrorInfo } from './detail';\nimport type { ResolvedErrorReason } from './reason';\nimport type { ErrorBody } from './status';\n\nexport function getErrorInfo(data: unknown): ErrorInfo | null {\n if (typeof data !== 'object' || data === null || !('error' in data)) return null;\n const { error } = data as ErrorBody;\n const errorInfo = error?.details?.find((d) => d['@type'] === DetailType.ERROR_INFO);\n return errorInfo ?? null;\n}\n\n/**\n * @example For axios:\n *\n * const { t } = useTranslation('error');\n *\n * const { message } = getErrorMessage(error.response.data, t);\n * toast.error(message);\n */\nexport function getErrorMessage<Ns extends Namespace = DefaultNamespace, KPrefix = undefined>(\n data: unknown,\n t: TFunction<Ns, KPrefix>\n): { reason: ResolvedErrorReason | null; message: string } {\n // 0. unknown error\n if (typeof data !== 'object' || data === null || !('error' in data)) {\n console.error('Unknown API error:', data);\n return { reason: null, message: t('UNKNOWN') };\n }\n\n const { error } = data as ErrorBody;\n\n // 1. from server via Accept-Language header or lng param\n const localizedMessage = error?.details?.find((d) => d['@type'] === DetailType.LOCALIZED_MESSAGE);\n if (localizedMessage) return { reason: null, message: localizedMessage.message };\n\n // 2. from business logic error\n const errorInfo = error?.details?.find((d) => d['@type'] === DetailType.ERROR_INFO);\n if (errorInfo) {\n return {\n reason: errorInfo.reason,\n message: t(errorInfo.reason, errorInfo.metadata),\n };\n }\n\n // 3. error message in english\n if (hasText(error.message)) return { reason: null, message: error.message };\n\n // 4. from server via status code\n if (error.status) return { reason: null, message: t(error.status) };\n\n // 5. unknown error\n console.error('Unknown API error:', data);\n return { reason: null, message: t('UNKNOWN') };\n}\n\n/**\n * @example For react-hook-form:\n *\n * const { setError } = useForm();\n * const fieldViolations = getFieldViolations(error.response.data);\n * fieldViolations.forEach((violation) => {\n * setError(violation.field, { message: violation.description });\n * });\n */\nexport function getFieldViolations(data: unknown): BadRequest['fieldViolations'] {\n if (typeof data !== 'object' || data === null || !('error' in data)) return [];\n const { error } = data as ErrorBody;\n const badRequest = error?.details?.find((d) => d['@type'] === DetailType.BAD_REQUEST);\n return badRequest?.fieldViolations ?? [];\n}\n"],"mappings":";AAAA,SAAS,eAAe;AAExB,SAA0B,kBAAkC;AAIrD,SAAS,aAAa,MAAiC;AAC5D,MAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,MAAO,QAAO;AAC5E,QAAM,EAAE,MAAM,IAAI;AAClB,QAAM,YAAY,OAAO,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,WAAW,UAAU;AAClF,SAAO,aAAa;AACtB;AAUO,SAAS,gBACd,MACA,GACyD;AAEzD,MAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,OAAO;AACnE,YAAQ,MAAM,sBAAsB,IAAI;AACxC,WAAO,EAAE,QAAQ,MAAM,SAAS,EAAE,SAAS,EAAE;AAAA,EAC/C;AAEA,QAAM,EAAE,MAAM,IAAI;AAGlB,QAAM,mBAAmB,OAAO,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,WAAW,iBAAiB;AAChG,MAAI,iBAAkB,QAAO,EAAE,QAAQ,MAAM,SAAS,iBAAiB,QAAQ;AAG/E,QAAM,YAAY,OAAO,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,WAAW,UAAU;AAClF,MAAI,WAAW;AACb,WAAO;AAAA,MACL,QAAQ,UAAU;AAAA,MAClB,SAAS,EAAE,UAAU,QAAQ,UAAU,QAAQ;AAAA,IACjD;AAAA,EACF;AAGA,MAAI,QAAQ,MAAM,OAAO,EAAG,QAAO,EAAE,QAAQ,MAAM,SAAS,MAAM,QAAQ;AAG1E,MAAI,MAAM,OAAQ,QAAO,EAAE,QAAQ,MAAM,SAAS,EAAE,MAAM,MAAM,EAAE;AAGlE,UAAQ,MAAM,sBAAsB,IAAI;AACxC,SAAO,EAAE,QAAQ,MAAM,SAAS,EAAE,SAAS,EAAE;AAC/C;AAWO,SAAS,mBAAmB,MAA8C;AAC/E,MAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,MAAO,QAAO,CAAC;AAC7E,QAAM,EAAE,MAAM,IAAI;AAClB,QAAM,aAAa,OAAO,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,WAAW,WAAW;AACpF,SAAO,YAAY,mBAAmB,CAAC;AACzC;","names":[]}
@@ -24,14 +24,39 @@ __export(google_one_tap_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(google_one_tap_exports);
26
26
  var script = null;
27
- function prompt({
27
+ function loadScript() {
28
+ return new Promise((resolve, reject) => {
29
+ if (script) {
30
+ if (window.google?.accounts?.id) {
31
+ resolve();
32
+ } else {
33
+ script.addEventListener("load", () => resolve());
34
+ script.addEventListener(
35
+ "error",
36
+ () => reject(new Error("Failed to load Google One Tap script"))
37
+ );
38
+ }
39
+ return;
40
+ }
41
+ script = document.createElement("script");
42
+ script.id = "google-one-tap";
43
+ script.async = true;
44
+ script.defer = true;
45
+ script.src = "https://accounts.google.com/gsi/client";
46
+ script.onload = () => resolve();
47
+ script.onerror = () => reject(new Error("Failed to load Google One Tap script"));
48
+ document.head.appendChild(script);
49
+ });
50
+ }
51
+ async function prompt({
28
52
  client_id,
29
53
  auto_select = false,
30
54
  use_fedcm_for_prompt = true,
31
- cancel_on_tap_outside = false,
32
- callback
55
+ cancel_on_tap_outside = false
33
56
  }) {
34
- const initializeAndPrompt = () => {
57
+ await loadScript();
58
+ return new Promise((resolve) => {
59
+ let settled = false;
35
60
  window.google.accounts.id.initialize({
36
61
  ux_mode: "popup",
37
62
  context: "signin",
@@ -39,26 +64,33 @@ function prompt({
39
64
  client_id,
40
65
  use_fedcm_for_prompt,
41
66
  cancel_on_tap_outside,
42
- callback,
43
- native_callback: callback
67
+ callback: (credential) => {
68
+ if (settled) return;
69
+ settled = true;
70
+ resolve({ authorized: true, credential });
71
+ },
72
+ native_callback: (credential) => {
73
+ if (settled) return;
74
+ settled = true;
75
+ resolve({ authorized: true, credential });
76
+ }
44
77
  });
45
- window.google?.accounts?.id?.prompt();
46
- };
47
- if (script) {
48
- if (window.google?.accounts?.id) {
49
- initializeAndPrompt();
50
- } else {
51
- script.addEventListener("load", initializeAndPrompt);
52
- }
53
- return;
54
- }
55
- script = document.createElement("script");
56
- script.id = "google-one-tap";
57
- script.async = true;
58
- script.defer = true;
59
- script.src = "https://accounts.google.com/gsi/client";
60
- script.onload = initializeAndPrompt;
61
- document.head.appendChild(script);
78
+ window.google.accounts.id.prompt((notification) => {
79
+ if (settled) return;
80
+ if (notification.isSkippedMoment() || notification.isDismissedMoment() && notification.getDismissedReason() !== "credential_returned") {
81
+ settled = true;
82
+ resolve({
83
+ authorized: false,
84
+ moment: {
85
+ momentType: notification.getMomentType(),
86
+ skipped: notification.isSkippedMoment(),
87
+ dismissed: notification.isDismissedMoment(),
88
+ dismissedReason: notification.getDismissedReason()
89
+ }
90
+ });
91
+ }
92
+ });
93
+ });
62
94
  }
63
95
  // Annotate the CommonJS export names for ESM import in node:
64
96
  0 && (module.exports = {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/google-one-tap/index.ts"],"sourcesContent":["import type { CredentialResponse, GoogleAccounts } from './types';\n\ndeclare global {\n interface Window {\n google: {\n accounts: GoogleAccounts;\n };\n }\n}\n\nexport type Props = {\n client_id: string;\n auto_select?: boolean;\n cancel_on_tap_outside?: boolean;\n use_fedcm_for_prompt?: boolean;\n callback: (response: CredentialResponse) => void | Promise<void>;\n};\n\nlet script: HTMLScriptElement | null = null;\n\n/** debug: chrome://settings/content/federatedIdentityApi */\nexport function prompt({\n client_id,\n auto_select = false,\n use_fedcm_for_prompt = true,\n cancel_on_tap_outside = false,\n callback,\n}: Props) {\n const initializeAndPrompt = () => {\n window.google.accounts.id.initialize({\n ux_mode: 'popup',\n context: 'signin',\n auto_select,\n client_id,\n use_fedcm_for_prompt,\n cancel_on_tap_outside,\n callback,\n native_callback: callback,\n });\n window.google?.accounts?.id?.prompt();\n };\n\n if (script) {\n if (window.google?.accounts?.id) {\n initializeAndPrompt();\n } else {\n script.addEventListener('load', initializeAndPrompt);\n }\n return;\n }\n\n script = document.createElement('script');\n script.id = 'google-one-tap';\n script.async = true;\n script.defer = true;\n script.src = 'https://accounts.google.com/gsi/client';\n script.onload = initializeAndPrompt;\n\n document.head.appendChild(script);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBA,IAAI,SAAmC;AAGhC,SAAS,OAAO;AAAA,EACrB;AAAA,EACA,cAAc;AAAA,EACd,uBAAuB;AAAA,EACvB,wBAAwB;AAAA,EACxB;AACF,GAAU;AACR,QAAM,sBAAsB,MAAM;AAChC,WAAO,OAAO,SAAS,GAAG,WAAW;AAAA,MACnC,SAAS;AAAA,MACT,SAAS;AAAA,MACT;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,iBAAiB;AAAA,IACnB,CAAC;AACD,WAAO,QAAQ,UAAU,IAAI,OAAO;AAAA,EACtC;AAEA,MAAI,QAAQ;AACV,QAAI,OAAO,QAAQ,UAAU,IAAI;AAC/B,0BAAoB;AAAA,IACtB,OAAO;AACL,aAAO,iBAAiB,QAAQ,mBAAmB;AAAA,IACrD;AACA;AAAA,EACF;AAEA,WAAS,SAAS,cAAc,QAAQ;AACxC,SAAO,KAAK;AACZ,SAAO,QAAQ;AACf,SAAO,QAAQ;AACf,SAAO,MAAM;AACb,SAAO,SAAS;AAEhB,WAAS,KAAK,YAAY,MAAM;AAClC;","names":[]}
1
+ {"version":3,"sources":["../../src/google-one-tap/index.ts"],"sourcesContent":["import type { CredentialResponse, GoogleAccounts, PromptMomentNotification } from './types';\n\ndeclare global {\n interface Window {\n google: {\n accounts: GoogleAccounts;\n };\n }\n}\n\nexport type Props = {\n client_id: string;\n auto_select?: boolean;\n cancel_on_tap_outside?: boolean;\n use_fedcm_for_prompt?: boolean;\n};\n\nexport type PromptMoment = {\n skipped: boolean;\n dismissed: boolean;\n momentType: ReturnType<PromptMomentNotification['getMomentType']>;\n dismissedReason: ReturnType<PromptMomentNotification['getDismissedReason']>;\n};\n\nexport type PromptResult =\n | { authorized: true; credential: CredentialResponse }\n | { authorized: false; moment: PromptMoment };\n\nlet script: HTMLScriptElement | null = null;\n\nfunction loadScript(): Promise<void> {\n return new Promise((resolve, reject) => {\n if (script) {\n if (window.google?.accounts?.id) {\n resolve();\n } else {\n script.addEventListener('load', () => resolve());\n script.addEventListener('error', () =>\n reject(new Error('Failed to load Google One Tap script'))\n );\n }\n return;\n }\n\n script = document.createElement('script');\n script.id = 'google-one-tap';\n script.async = true;\n script.defer = true;\n script.src = 'https://accounts.google.com/gsi/client';\n script.onload = () => resolve();\n script.onerror = () => reject(new Error('Failed to load Google One Tap script'));\n\n document.head.appendChild(script);\n });\n}\n\n/** debug: chrome://settings/content/federatedIdentityApi */\nexport async function prompt({\n client_id,\n auto_select = false,\n use_fedcm_for_prompt = true,\n cancel_on_tap_outside = false,\n}: Props): Promise<PromptResult> {\n await loadScript();\n\n return new Promise<PromptResult>((resolve) => {\n let settled = false;\n\n window.google.accounts.id.initialize({\n ux_mode: 'popup',\n context: 'signin',\n auto_select,\n client_id,\n use_fedcm_for_prompt,\n cancel_on_tap_outside,\n callback: (credential) => {\n if (settled) return;\n settled = true;\n resolve({ authorized: true, credential });\n },\n native_callback: (credential) => {\n if (settled) return;\n settled = true;\n resolve({ authorized: true, credential });\n },\n });\n\n window.google.accounts.id.prompt((notification) => {\n if (settled) return;\n\n if (\n notification.isSkippedMoment() ||\n (notification.isDismissedMoment() &&\n notification.getDismissedReason() !== 'credential_returned')\n ) {\n settled = true;\n resolve({\n authorized: false,\n moment: {\n momentType: notification.getMomentType(),\n skipped: notification.isSkippedMoment(),\n dismissed: notification.isDismissedMoment(),\n dismissedReason: notification.getDismissedReason(),\n },\n });\n }\n });\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA4BA,IAAI,SAAmC;AAEvC,SAAS,aAA4B;AACnC,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,QAAI,QAAQ;AACV,UAAI,OAAO,QAAQ,UAAU,IAAI;AAC/B,gBAAQ;AAAA,MACV,OAAO;AACL,eAAO,iBAAiB,QAAQ,MAAM,QAAQ,CAAC;AAC/C,eAAO;AAAA,UAAiB;AAAA,UAAS,MAC/B,OAAO,IAAI,MAAM,sCAAsC,CAAC;AAAA,QAC1D;AAAA,MACF;AACA;AAAA,IACF;AAEA,aAAS,SAAS,cAAc,QAAQ;AACxC,WAAO,KAAK;AACZ,WAAO,QAAQ;AACf,WAAO,QAAQ;AACf,WAAO,MAAM;AACb,WAAO,SAAS,MAAM,QAAQ;AAC9B,WAAO,UAAU,MAAM,OAAO,IAAI,MAAM,sCAAsC,CAAC;AAE/E,aAAS,KAAK,YAAY,MAAM;AAAA,EAClC,CAAC;AACH;AAGA,eAAsB,OAAO;AAAA,EAC3B;AAAA,EACA,cAAc;AAAA,EACd,uBAAuB;AAAA,EACvB,wBAAwB;AAC1B,GAAiC;AAC/B,QAAM,WAAW;AAEjB,SAAO,IAAI,QAAsB,CAAC,YAAY;AAC5C,QAAI,UAAU;AAEd,WAAO,OAAO,SAAS,GAAG,WAAW;AAAA,MACnC,SAAS;AAAA,MACT,SAAS;AAAA,MACT;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,UAAU,CAAC,eAAe;AACxB,YAAI,QAAS;AACb,kBAAU;AACV,gBAAQ,EAAE,YAAY,MAAM,WAAW,CAAC;AAAA,MAC1C;AAAA,MACA,iBAAiB,CAAC,eAAe;AAC/B,YAAI,QAAS;AACb,kBAAU;AACV,gBAAQ,EAAE,YAAY,MAAM,WAAW,CAAC;AAAA,MAC1C;AAAA,IACF,CAAC;AAED,WAAO,OAAO,SAAS,GAAG,OAAO,CAAC,iBAAiB;AACjD,UAAI,QAAS;AAEb,UACE,aAAa,gBAAgB,KAC5B,aAAa,kBAAkB,KAC9B,aAAa,mBAAmB,MAAM,uBACxC;AACA,kBAAU;AACV,gBAAQ;AAAA,UACN,YAAY;AAAA,UACZ,QAAQ;AAAA,YACN,YAAY,aAAa,cAAc;AAAA,YACvC,SAAS,aAAa,gBAAgB;AAAA,YACtC,WAAW,aAAa,kBAAkB;AAAA,YAC1C,iBAAiB,aAAa,mBAAmB;AAAA,UACnD;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AACH;","names":[]}
@@ -1,4 +1,4 @@
1
- import { GoogleAccounts, CredentialResponse } from './types.cjs';
1
+ import { GoogleAccounts, PromptMomentNotification, CredentialResponse } from './types.cjs';
2
2
 
3
3
  declare global {
4
4
  interface Window {
@@ -12,9 +12,21 @@ type Props = {
12
12
  auto_select?: boolean;
13
13
  cancel_on_tap_outside?: boolean;
14
14
  use_fedcm_for_prompt?: boolean;
15
- callback: (response: CredentialResponse) => void | Promise<void>;
15
+ };
16
+ type PromptMoment = {
17
+ skipped: boolean;
18
+ dismissed: boolean;
19
+ momentType: ReturnType<PromptMomentNotification['getMomentType']>;
20
+ dismissedReason: ReturnType<PromptMomentNotification['getDismissedReason']>;
21
+ };
22
+ type PromptResult = {
23
+ authorized: true;
24
+ credential: CredentialResponse;
25
+ } | {
26
+ authorized: false;
27
+ moment: PromptMoment;
16
28
  };
17
29
  /** debug: chrome://settings/content/federatedIdentityApi */
18
- declare function prompt({ client_id, auto_select, use_fedcm_for_prompt, cancel_on_tap_outside, callback, }: Props): void;
30
+ declare function prompt({ client_id, auto_select, use_fedcm_for_prompt, cancel_on_tap_outside, }: Props): Promise<PromptResult>;
19
31
 
20
- export { type Props, prompt };
32
+ export { type PromptMoment, type PromptResult, type Props, prompt };
@@ -1,4 +1,4 @@
1
- import { GoogleAccounts, CredentialResponse } from './types.js';
1
+ import { GoogleAccounts, PromptMomentNotification, CredentialResponse } from './types.js';
2
2
 
3
3
  declare global {
4
4
  interface Window {
@@ -12,9 +12,21 @@ type Props = {
12
12
  auto_select?: boolean;
13
13
  cancel_on_tap_outside?: boolean;
14
14
  use_fedcm_for_prompt?: boolean;
15
- callback: (response: CredentialResponse) => void | Promise<void>;
15
+ };
16
+ type PromptMoment = {
17
+ skipped: boolean;
18
+ dismissed: boolean;
19
+ momentType: ReturnType<PromptMomentNotification['getMomentType']>;
20
+ dismissedReason: ReturnType<PromptMomentNotification['getDismissedReason']>;
21
+ };
22
+ type PromptResult = {
23
+ authorized: true;
24
+ credential: CredentialResponse;
25
+ } | {
26
+ authorized: false;
27
+ moment: PromptMoment;
16
28
  };
17
29
  /** debug: chrome://settings/content/federatedIdentityApi */
18
- declare function prompt({ client_id, auto_select, use_fedcm_for_prompt, cancel_on_tap_outside, callback, }: Props): void;
30
+ declare function prompt({ client_id, auto_select, use_fedcm_for_prompt, cancel_on_tap_outside, }: Props): Promise<PromptResult>;
19
31
 
20
- export { type Props, prompt };
32
+ export { type PromptMoment, type PromptResult, type Props, prompt };
@@ -1,13 +1,38 @@
1
1
  // src/google-one-tap/index.ts
2
2
  var script = null;
3
- function prompt({
3
+ function loadScript() {
4
+ return new Promise((resolve, reject) => {
5
+ if (script) {
6
+ if (window.google?.accounts?.id) {
7
+ resolve();
8
+ } else {
9
+ script.addEventListener("load", () => resolve());
10
+ script.addEventListener(
11
+ "error",
12
+ () => reject(new Error("Failed to load Google One Tap script"))
13
+ );
14
+ }
15
+ return;
16
+ }
17
+ script = document.createElement("script");
18
+ script.id = "google-one-tap";
19
+ script.async = true;
20
+ script.defer = true;
21
+ script.src = "https://accounts.google.com/gsi/client";
22
+ script.onload = () => resolve();
23
+ script.onerror = () => reject(new Error("Failed to load Google One Tap script"));
24
+ document.head.appendChild(script);
25
+ });
26
+ }
27
+ async function prompt({
4
28
  client_id,
5
29
  auto_select = false,
6
30
  use_fedcm_for_prompt = true,
7
- cancel_on_tap_outside = false,
8
- callback
31
+ cancel_on_tap_outside = false
9
32
  }) {
10
- const initializeAndPrompt = () => {
33
+ await loadScript();
34
+ return new Promise((resolve) => {
35
+ let settled = false;
11
36
  window.google.accounts.id.initialize({
12
37
  ux_mode: "popup",
13
38
  context: "signin",
@@ -15,26 +40,33 @@ function prompt({
15
40
  client_id,
16
41
  use_fedcm_for_prompt,
17
42
  cancel_on_tap_outside,
18
- callback,
19
- native_callback: callback
43
+ callback: (credential) => {
44
+ if (settled) return;
45
+ settled = true;
46
+ resolve({ authorized: true, credential });
47
+ },
48
+ native_callback: (credential) => {
49
+ if (settled) return;
50
+ settled = true;
51
+ resolve({ authorized: true, credential });
52
+ }
20
53
  });
21
- window.google?.accounts?.id?.prompt();
22
- };
23
- if (script) {
24
- if (window.google?.accounts?.id) {
25
- initializeAndPrompt();
26
- } else {
27
- script.addEventListener("load", initializeAndPrompt);
28
- }
29
- return;
30
- }
31
- script = document.createElement("script");
32
- script.id = "google-one-tap";
33
- script.async = true;
34
- script.defer = true;
35
- script.src = "https://accounts.google.com/gsi/client";
36
- script.onload = initializeAndPrompt;
37
- document.head.appendChild(script);
54
+ window.google.accounts.id.prompt((notification) => {
55
+ if (settled) return;
56
+ if (notification.isSkippedMoment() || notification.isDismissedMoment() && notification.getDismissedReason() !== "credential_returned") {
57
+ settled = true;
58
+ resolve({
59
+ authorized: false,
60
+ moment: {
61
+ momentType: notification.getMomentType(),
62
+ skipped: notification.isSkippedMoment(),
63
+ dismissed: notification.isDismissedMoment(),
64
+ dismissedReason: notification.getDismissedReason()
65
+ }
66
+ });
67
+ }
68
+ });
69
+ });
38
70
  }
39
71
  export {
40
72
  prompt
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/google-one-tap/index.ts"],"sourcesContent":["import type { CredentialResponse, GoogleAccounts } from './types';\n\ndeclare global {\n interface Window {\n google: {\n accounts: GoogleAccounts;\n };\n }\n}\n\nexport type Props = {\n client_id: string;\n auto_select?: boolean;\n cancel_on_tap_outside?: boolean;\n use_fedcm_for_prompt?: boolean;\n callback: (response: CredentialResponse) => void | Promise<void>;\n};\n\nlet script: HTMLScriptElement | null = null;\n\n/** debug: chrome://settings/content/federatedIdentityApi */\nexport function prompt({\n client_id,\n auto_select = false,\n use_fedcm_for_prompt = true,\n cancel_on_tap_outside = false,\n callback,\n}: Props) {\n const initializeAndPrompt = () => {\n window.google.accounts.id.initialize({\n ux_mode: 'popup',\n context: 'signin',\n auto_select,\n client_id,\n use_fedcm_for_prompt,\n cancel_on_tap_outside,\n callback,\n native_callback: callback,\n });\n window.google?.accounts?.id?.prompt();\n };\n\n if (script) {\n if (window.google?.accounts?.id) {\n initializeAndPrompt();\n } else {\n script.addEventListener('load', initializeAndPrompt);\n }\n return;\n }\n\n script = document.createElement('script');\n script.id = 'google-one-tap';\n script.async = true;\n script.defer = true;\n script.src = 'https://accounts.google.com/gsi/client';\n script.onload = initializeAndPrompt;\n\n document.head.appendChild(script);\n}\n"],"mappings":";AAkBA,IAAI,SAAmC;AAGhC,SAAS,OAAO;AAAA,EACrB;AAAA,EACA,cAAc;AAAA,EACd,uBAAuB;AAAA,EACvB,wBAAwB;AAAA,EACxB;AACF,GAAU;AACR,QAAM,sBAAsB,MAAM;AAChC,WAAO,OAAO,SAAS,GAAG,WAAW;AAAA,MACnC,SAAS;AAAA,MACT,SAAS;AAAA,MACT;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,iBAAiB;AAAA,IACnB,CAAC;AACD,WAAO,QAAQ,UAAU,IAAI,OAAO;AAAA,EACtC;AAEA,MAAI,QAAQ;AACV,QAAI,OAAO,QAAQ,UAAU,IAAI;AAC/B,0BAAoB;AAAA,IACtB,OAAO;AACL,aAAO,iBAAiB,QAAQ,mBAAmB;AAAA,IACrD;AACA;AAAA,EACF;AAEA,WAAS,SAAS,cAAc,QAAQ;AACxC,SAAO,KAAK;AACZ,SAAO,QAAQ;AACf,SAAO,QAAQ;AACf,SAAO,MAAM;AACb,SAAO,SAAS;AAEhB,WAAS,KAAK,YAAY,MAAM;AAClC;","names":[]}
1
+ {"version":3,"sources":["../../src/google-one-tap/index.ts"],"sourcesContent":["import type { CredentialResponse, GoogleAccounts, PromptMomentNotification } from './types';\n\ndeclare global {\n interface Window {\n google: {\n accounts: GoogleAccounts;\n };\n }\n}\n\nexport type Props = {\n client_id: string;\n auto_select?: boolean;\n cancel_on_tap_outside?: boolean;\n use_fedcm_for_prompt?: boolean;\n};\n\nexport type PromptMoment = {\n skipped: boolean;\n dismissed: boolean;\n momentType: ReturnType<PromptMomentNotification['getMomentType']>;\n dismissedReason: ReturnType<PromptMomentNotification['getDismissedReason']>;\n};\n\nexport type PromptResult =\n | { authorized: true; credential: CredentialResponse }\n | { authorized: false; moment: PromptMoment };\n\nlet script: HTMLScriptElement | null = null;\n\nfunction loadScript(): Promise<void> {\n return new Promise((resolve, reject) => {\n if (script) {\n if (window.google?.accounts?.id) {\n resolve();\n } else {\n script.addEventListener('load', () => resolve());\n script.addEventListener('error', () =>\n reject(new Error('Failed to load Google One Tap script'))\n );\n }\n return;\n }\n\n script = document.createElement('script');\n script.id = 'google-one-tap';\n script.async = true;\n script.defer = true;\n script.src = 'https://accounts.google.com/gsi/client';\n script.onload = () => resolve();\n script.onerror = () => reject(new Error('Failed to load Google One Tap script'));\n\n document.head.appendChild(script);\n });\n}\n\n/** debug: chrome://settings/content/federatedIdentityApi */\nexport async function prompt({\n client_id,\n auto_select = false,\n use_fedcm_for_prompt = true,\n cancel_on_tap_outside = false,\n}: Props): Promise<PromptResult> {\n await loadScript();\n\n return new Promise<PromptResult>((resolve) => {\n let settled = false;\n\n window.google.accounts.id.initialize({\n ux_mode: 'popup',\n context: 'signin',\n auto_select,\n client_id,\n use_fedcm_for_prompt,\n cancel_on_tap_outside,\n callback: (credential) => {\n if (settled) return;\n settled = true;\n resolve({ authorized: true, credential });\n },\n native_callback: (credential) => {\n if (settled) return;\n settled = true;\n resolve({ authorized: true, credential });\n },\n });\n\n window.google.accounts.id.prompt((notification) => {\n if (settled) return;\n\n if (\n notification.isSkippedMoment() ||\n (notification.isDismissedMoment() &&\n notification.getDismissedReason() !== 'credential_returned')\n ) {\n settled = true;\n resolve({\n authorized: false,\n moment: {\n momentType: notification.getMomentType(),\n skipped: notification.isSkippedMoment(),\n dismissed: notification.isDismissedMoment(),\n dismissedReason: notification.getDismissedReason(),\n },\n });\n }\n });\n });\n}\n"],"mappings":";AA4BA,IAAI,SAAmC;AAEvC,SAAS,aAA4B;AACnC,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,QAAI,QAAQ;AACV,UAAI,OAAO,QAAQ,UAAU,IAAI;AAC/B,gBAAQ;AAAA,MACV,OAAO;AACL,eAAO,iBAAiB,QAAQ,MAAM,QAAQ,CAAC;AAC/C,eAAO;AAAA,UAAiB;AAAA,UAAS,MAC/B,OAAO,IAAI,MAAM,sCAAsC,CAAC;AAAA,QAC1D;AAAA,MACF;AACA;AAAA,IACF;AAEA,aAAS,SAAS,cAAc,QAAQ;AACxC,WAAO,KAAK;AACZ,WAAO,QAAQ;AACf,WAAO,QAAQ;AACf,WAAO,MAAM;AACb,WAAO,SAAS,MAAM,QAAQ;AAC9B,WAAO,UAAU,MAAM,OAAO,IAAI,MAAM,sCAAsC,CAAC;AAE/E,aAAS,KAAK,YAAY,MAAM;AAAA,EAClC,CAAC;AACH;AAGA,eAAsB,OAAO;AAAA,EAC3B;AAAA,EACA,cAAc;AAAA,EACd,uBAAuB;AAAA,EACvB,wBAAwB;AAC1B,GAAiC;AAC/B,QAAM,WAAW;AAEjB,SAAO,IAAI,QAAsB,CAAC,YAAY;AAC5C,QAAI,UAAU;AAEd,WAAO,OAAO,SAAS,GAAG,WAAW;AAAA,MACnC,SAAS;AAAA,MACT,SAAS;AAAA,MACT;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,UAAU,CAAC,eAAe;AACxB,YAAI,QAAS;AACb,kBAAU;AACV,gBAAQ,EAAE,YAAY,MAAM,WAAW,CAAC;AAAA,MAC1C;AAAA,MACA,iBAAiB,CAAC,eAAe;AAC/B,YAAI,QAAS;AACb,kBAAU;AACV,gBAAQ,EAAE,YAAY,MAAM,WAAW,CAAC;AAAA,MAC1C;AAAA,IACF,CAAC;AAED,WAAO,OAAO,SAAS,GAAG,OAAO,CAAC,iBAAiB;AACjD,UAAI,QAAS;AAEb,UACE,aAAa,gBAAgB,KAC5B,aAAa,kBAAkB,KAC9B,aAAa,mBAAmB,MAAM,uBACxC;AACA,kBAAU;AACV,gBAAQ;AAAA,UACN,YAAY;AAAA,UACZ,QAAQ;AAAA,YACN,YAAY,aAAa,cAAc;AAAA,YACvC,SAAS,aAAa,gBAAgB;AAAA,YACtC,WAAW,aAAa,kBAAkB;AAAA,YAC1C,iBAAiB,aAAa,mBAAmB;AAAA,UACnD;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AACH;","names":[]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hono/csrf.ts"],"sourcesContent":["import type { MiddlewareHandler } from 'hono';\nimport { timingSafeEqual } from 'crypto';\nimport { getCookie } from 'hono/cookie';\nimport { METHOD_NAME_ALL } from 'hono/router';\nimport { RegExpRouter } from 'hono/router/reg-exp-router';\nimport { SmartRouter } from 'hono/router/smart-router';\nimport { TrieRouter } from 'hono/router/trie-router';\nimport { Status } from '../error/status';\n\ntype HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';\n\nexport type CSRFIgnoreRule = string | { path: string; methods?: [HTTPMethod, ...HTTPMethod[]] };\n\nexport interface CSRFConfig {\n /**\n * Cookie name for CSRF token\n * @default 'XSRF-TOKEN'\n */\n cookieName?: string;\n\n /**\n * Header name for CSRF token\n * @default 'X-XSRF-TOKEN'\n */\n headerName?: string;\n\n /**\n * Ignore rules for specific paths and methods\n * @example\n * [\n * { path: '/api/webhook/*', methods: ['POST'] },\n * { path: '/auth/apple/callback' }, // ignores all methods\n * ]\n */\n ignores?: CSRFIgnoreRule[];\n\n /**\n * Skip CSRF check for these methods\n * @default ['GET', 'HEAD', 'OPTIONS']\n */\n safeMethods?: HTTPMethod[];\n\n /**\n * Origin allowed to bypass CSRF check\n * @default undefined\n */\n origin?: string[];\n\n /**\n * Sec-Fetch-Site allowed to bypass CSRF check\n * @default undefined\n */\n secFetchSite?: Array<'same-origin' | 'same-site' | 'none' | 'cross-origin'>;\n\n /**\n * Custom error message\n * @default 'CSRF token validation failed'\n */\n errorMessage?: string;\n}\n\n/** use timing safe compare to prevent timing attack */\nfunction safeCompare(a: string, b: string): boolean {\n if (typeof a !== 'string' || typeof b !== 'string') return false;\n if (a.length === 0 || b.length === 0) return false;\n\n const bufferA = Buffer.from(a, 'utf-8');\n const bufferB = Buffer.from(b, 'utf-8');\n if (bufferA.length !== bufferB.length) return false;\n return timingSafeEqual(bufferA, bufferB);\n}\n\n/**\n * Create CSRF protection middleware\n *\n * @example\n * ```ts\n * import { Hono } from 'hono';\n * import { csrf } from '@shware/http/hono';\n *\n * const app = new Hono();\n *\n * // basic usage\n * app.use(csrf());\n *\n * // with configuration\n * app.use(csrf({\n * cookieName: 'csrf-token',\n * headerName: 'X-CSRF-Token',\n * ignores: [\n * { path: '/api/webhook/*', methods: ['POST'] },\n * { path: '/auth/apple/callback' },\n * ]\n * }));\n * ```\n */\nexport function csrf(config: CSRFConfig = {}): MiddlewareHandler {\n const cookieName = config.cookieName ?? 'XSRF-TOKEN';\n const headerName = config.headerName ?? 'X-XSRF-TOKEN';\n const safeMethods = new Set(config.safeMethods ?? ['GET', 'HEAD', 'OPTIONS']);\n const errorMessage = config.errorMessage ?? 'CSRF token validation failed';\n\n // initialize router for matching ignore rules\n const router = new SmartRouter<boolean>({\n routers: [new RegExpRouter(), new TrieRouter()],\n });\n\n // register ignore rules\n if (config.ignores) {\n for (const rule of config.ignores) {\n if (typeof rule === 'string') {\n router.add(METHOD_NAME_ALL, rule, true);\n } else if (rule.methods && rule.methods.length > 0) {\n for (const method of rule.methods) {\n router.add(method, rule.path, true);\n }\n } else {\n // if no methods are specified, ignore all methods\n router.add(METHOD_NAME_ALL, rule.path, true);\n }\n }\n }\n\n // return middleware\n return async (c, next) => {\n const method = c.req.method;\n const path = c.req.path;\n\n // check if the request should be ignored\n // 1. ignore safe methods\n if (safeMethods.has(method)) {\n await next();\n return;\n }\n\n // 2. ignore configured origin\n if (config.origin && config.origin.includes(c.req.header('origin') ?? '')) {\n await next();\n return;\n }\n\n // 3. ignore configured secFetchSite\n if (\n config.secFetchSite &&\n config.secFetchSite.includes((c.req.header('sec-fetch-site') ?? '') as never)\n ) {\n await next();\n return;\n }\n\n // 4. ignore configured ignore rules\n const [matched] = router.match(method, path);\n if (matched.length > 0) {\n await next();\n return;\n }\n\n const cookieToken = getCookie(c, cookieName);\n const headerToken = c.req.header(headerName);\n\n if (!cookieToken || !headerToken) {\n throw Status.permissionDenied(errorMessage).error();\n }\n\n if (!safeCompare(cookieToken, headerToken)) {\n throw Status.permissionDenied(errorMessage).error();\n }\n\n await next();\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAAgC;AAChC,oBAA0B;AAC1B,oBAAgC;AAChC,4BAA6B;AAC7B,0BAA4B;AAC5B,yBAA2B;AAC3B,oBAAuB;AAuDvB,SAAS,YAAY,GAAW,GAAoB;AAClD,MAAI,OAAO,MAAM,YAAY,OAAO,MAAM,SAAU,QAAO;AAC3D,MAAI,EAAE,WAAW,KAAK,EAAE,WAAW,EAAG,QAAO;AAE7C,QAAM,UAAU,OAAO,KAAK,GAAG,OAAO;AACtC,QAAM,UAAU,OAAO,KAAK,GAAG,OAAO;AACtC,MAAI,QAAQ,WAAW,QAAQ,OAAQ,QAAO;AAC9C,aAAO,+BAAgB,SAAS,OAAO;AACzC;AA0BO,SAAS,KAAK,SAAqB,CAAC,GAAsB;AAC/D,QAAM,aAAa,OAAO,cAAc;AACxC,QAAM,aAAa,OAAO,cAAc;AACxC,QAAM,cAAc,IAAI,IAAI,OAAO,eAAe,CAAC,OAAO,QAAQ,SAAS,CAAC;AAC5E,QAAM,eAAe,OAAO,gBAAgB;AAG5C,QAAM,SAAS,IAAI,gCAAqB;AAAA,IACtC,SAAS,CAAC,IAAI,mCAAa,GAAG,IAAI,8BAAW,CAAC;AAAA,EAChD,CAAC;AAGD,MAAI,OAAO,SAAS;AAClB,eAAW,QAAQ,OAAO,SAAS;AACjC,UAAI,OAAO,SAAS,UAAU;AAC5B,eAAO,IAAI,+BAAiB,MAAM,IAAI;AAAA,MACxC,WAAW,KAAK,WAAW,KAAK,QAAQ,SAAS,GAAG;AAClD,mBAAW,UAAU,KAAK,SAAS;AACjC,iBAAO,IAAI,QAAQ,KAAK,MAAM,IAAI;AAAA,QACpC;AAAA,MACF,OAAO;AAEL,eAAO,IAAI,+BAAiB,KAAK,MAAM,IAAI;AAAA,MAC7C;AAAA,IACF;AAAA,EACF;AAGA,SAAO,OAAO,GAAG,SAAS;AACxB,UAAM,SAAS,EAAE,IAAI;AACrB,UAAM,OAAO,EAAE,IAAI;AAInB,QAAI,YAAY,IAAI,MAAM,GAAG;AAC3B,YAAM,KAAK;AACX;AAAA,IACF;AAGA,QAAI,OAAO,UAAU,OAAO,OAAO,SAAS,EAAE,IAAI,OAAO,QAAQ,KAAK,EAAE,GAAG;AACzE,YAAM,KAAK;AACX;AAAA,IACF;AAGA,QACE,OAAO,gBACP,OAAO,aAAa,SAAU,EAAE,IAAI,OAAO,gBAAgB,KAAK,EAAY,GAC5E;AACA,YAAM,KAAK;AACX;AAAA,IACF;AAGA,UAAM,CAAC,OAAO,IAAI,OAAO,MAAM,QAAQ,IAAI;AAC3C,QAAI,QAAQ,SAAS,GAAG;AACtB,YAAM,KAAK;AACX;AAAA,IACF;AAEA,UAAM,kBAAc,yBAAU,GAAG,UAAU;AAC3C,UAAM,cAAc,EAAE,IAAI,OAAO,UAAU;AAE3C,QAAI,CAAC,eAAe,CAAC,aAAa;AAChC,YAAM,qBAAO,iBAAiB,YAAY,EAAE,MAAM;AAAA,IACpD;AAEA,QAAI,CAAC,YAAY,aAAa,WAAW,GAAG;AAC1C,YAAM,qBAAO,iBAAiB,YAAY,EAAE,MAAM;AAAA,IACpD;AAEA,UAAM,KAAK;AAAA,EACb;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/hono/csrf.ts"],"sourcesContent":["import { timingSafeEqual } from 'crypto';\nimport type { MiddlewareHandler } from 'hono';\nimport { getCookie } from 'hono/cookie';\nimport { METHOD_NAME_ALL } from 'hono/router';\nimport { RegExpRouter } from 'hono/router/reg-exp-router';\nimport { SmartRouter } from 'hono/router/smart-router';\nimport { TrieRouter } from 'hono/router/trie-router';\nimport { Status } from '../error/status';\n\ntype HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';\n\nexport type CSRFIgnoreRule = string | { path: string; methods?: [HTTPMethod, ...HTTPMethod[]] };\n\nexport interface CSRFConfig {\n /**\n * Cookie name for CSRF token\n * @default 'XSRF-TOKEN'\n */\n cookieName?: string;\n\n /**\n * Header name for CSRF token\n * @default 'X-XSRF-TOKEN'\n */\n headerName?: string;\n\n /**\n * Ignore rules for specific paths and methods\n * @example\n * [\n * { path: '/api/webhook/*', methods: ['POST'] },\n * { path: '/auth/apple/callback' }, // ignores all methods\n * ]\n */\n ignores?: CSRFIgnoreRule[];\n\n /**\n * Skip CSRF check for these methods\n * @default ['GET', 'HEAD', 'OPTIONS']\n */\n safeMethods?: HTTPMethod[];\n\n /**\n * Origin allowed to bypass CSRF check\n * @default undefined\n */\n origin?: string[];\n\n /**\n * Sec-Fetch-Site allowed to bypass CSRF check\n * @default undefined\n */\n secFetchSite?: Array<'same-origin' | 'same-site' | 'none' | 'cross-origin'>;\n\n /**\n * Custom error message\n * @default 'CSRF token validation failed'\n */\n errorMessage?: string;\n}\n\n/** use timing safe compare to prevent timing attack */\nfunction safeCompare(a: string, b: string): boolean {\n if (typeof a !== 'string' || typeof b !== 'string') return false;\n if (a.length === 0 || b.length === 0) return false;\n\n const bufferA = Buffer.from(a, 'utf-8');\n const bufferB = Buffer.from(b, 'utf-8');\n if (bufferA.length !== bufferB.length) return false;\n return timingSafeEqual(bufferA, bufferB);\n}\n\n/**\n * Create CSRF protection middleware\n *\n * @example\n * ```ts\n * import { Hono } from 'hono';\n * import { csrf } from '@shware/http/hono';\n *\n * const app = new Hono();\n *\n * // basic usage\n * app.use(csrf());\n *\n * // with configuration\n * app.use(csrf({\n * cookieName: 'csrf-token',\n * headerName: 'X-CSRF-Token',\n * ignores: [\n * { path: '/api/webhook/*', methods: ['POST'] },\n * { path: '/auth/apple/callback' },\n * ]\n * }));\n * ```\n */\nexport function csrf(config: CSRFConfig = {}): MiddlewareHandler {\n const cookieName = config.cookieName ?? 'XSRF-TOKEN';\n const headerName = config.headerName ?? 'X-XSRF-TOKEN';\n const safeMethods = new Set(config.safeMethods ?? ['GET', 'HEAD', 'OPTIONS']);\n const errorMessage = config.errorMessage ?? 'CSRF token validation failed';\n\n // initialize router for matching ignore rules\n const router = new SmartRouter<boolean>({\n routers: [new RegExpRouter(), new TrieRouter()],\n });\n\n // register ignore rules\n if (config.ignores) {\n for (const rule of config.ignores) {\n if (typeof rule === 'string') {\n router.add(METHOD_NAME_ALL, rule, true);\n } else if (rule.methods && rule.methods.length > 0) {\n for (const method of rule.methods) {\n router.add(method, rule.path, true);\n }\n } else {\n // if no methods are specified, ignore all methods\n router.add(METHOD_NAME_ALL, rule.path, true);\n }\n }\n }\n\n // return middleware\n return async (c, next) => {\n const method = c.req.method;\n const path = c.req.path;\n\n // check if the request should be ignored\n // 1. ignore safe methods\n if (safeMethods.has(method)) {\n await next();\n return;\n }\n\n // 2. ignore configured origin\n if (config.origin && config.origin.includes(c.req.header('origin') ?? '')) {\n await next();\n return;\n }\n\n // 3. ignore configured secFetchSite\n if (\n config.secFetchSite &&\n config.secFetchSite.includes((c.req.header('sec-fetch-site') ?? '') as never)\n ) {\n await next();\n return;\n }\n\n // 4. ignore configured ignore rules\n const [matched] = router.match(method, path);\n if (matched.length > 0) {\n await next();\n return;\n }\n\n const cookieToken = getCookie(c, cookieName);\n const headerToken = c.req.header(headerName);\n\n if (!cookieToken || !headerToken) {\n throw Status.permissionDenied(errorMessage).error();\n }\n\n if (!safeCompare(cookieToken, headerToken)) {\n throw Status.permissionDenied(errorMessage).error();\n }\n\n await next();\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAgC;AAEhC,oBAA0B;AAC1B,oBAAgC;AAChC,4BAA6B;AAC7B,0BAA4B;AAC5B,yBAA2B;AAC3B,oBAAuB;AAuDvB,SAAS,YAAY,GAAW,GAAoB;AAClD,MAAI,OAAO,MAAM,YAAY,OAAO,MAAM,SAAU,QAAO;AAC3D,MAAI,EAAE,WAAW,KAAK,EAAE,WAAW,EAAG,QAAO;AAE7C,QAAM,UAAU,OAAO,KAAK,GAAG,OAAO;AACtC,QAAM,UAAU,OAAO,KAAK,GAAG,OAAO;AACtC,MAAI,QAAQ,WAAW,QAAQ,OAAQ,QAAO;AAC9C,aAAO,+BAAgB,SAAS,OAAO;AACzC;AA0BO,SAAS,KAAK,SAAqB,CAAC,GAAsB;AAC/D,QAAM,aAAa,OAAO,cAAc;AACxC,QAAM,aAAa,OAAO,cAAc;AACxC,QAAM,cAAc,IAAI,IAAI,OAAO,eAAe,CAAC,OAAO,QAAQ,SAAS,CAAC;AAC5E,QAAM,eAAe,OAAO,gBAAgB;AAG5C,QAAM,SAAS,IAAI,gCAAqB;AAAA,IACtC,SAAS,CAAC,IAAI,mCAAa,GAAG,IAAI,8BAAW,CAAC;AAAA,EAChD,CAAC;AAGD,MAAI,OAAO,SAAS;AAClB,eAAW,QAAQ,OAAO,SAAS;AACjC,UAAI,OAAO,SAAS,UAAU;AAC5B,eAAO,IAAI,+BAAiB,MAAM,IAAI;AAAA,MACxC,WAAW,KAAK,WAAW,KAAK,QAAQ,SAAS,GAAG;AAClD,mBAAW,UAAU,KAAK,SAAS;AACjC,iBAAO,IAAI,QAAQ,KAAK,MAAM,IAAI;AAAA,QACpC;AAAA,MACF,OAAO;AAEL,eAAO,IAAI,+BAAiB,KAAK,MAAM,IAAI;AAAA,MAC7C;AAAA,IACF;AAAA,EACF;AAGA,SAAO,OAAO,GAAG,SAAS;AACxB,UAAM,SAAS,EAAE,IAAI;AACrB,UAAM,OAAO,EAAE,IAAI;AAInB,QAAI,YAAY,IAAI,MAAM,GAAG;AAC3B,YAAM,KAAK;AACX;AAAA,IACF;AAGA,QAAI,OAAO,UAAU,OAAO,OAAO,SAAS,EAAE,IAAI,OAAO,QAAQ,KAAK,EAAE,GAAG;AACzE,YAAM,KAAK;AACX;AAAA,IACF;AAGA,QACE,OAAO,gBACP,OAAO,aAAa,SAAU,EAAE,IAAI,OAAO,gBAAgB,KAAK,EAAY,GAC5E;AACA,YAAM,KAAK;AACX;AAAA,IACF;AAGA,UAAM,CAAC,OAAO,IAAI,OAAO,MAAM,QAAQ,IAAI;AAC3C,QAAI,QAAQ,SAAS,GAAG;AACtB,YAAM,KAAK;AACX;AAAA,IACF;AAEA,UAAM,kBAAc,yBAAU,GAAG,UAAU;AAC3C,UAAM,cAAc,EAAE,IAAI,OAAO,UAAU;AAE3C,QAAI,CAAC,eAAe,CAAC,aAAa;AAChC,YAAM,qBAAO,iBAAiB,YAAY,EAAE,MAAM;AAAA,IACpD;AAEA,QAAI,CAAC,YAAY,aAAa,WAAW,GAAG;AAC1C,YAAM,qBAAO,iBAAiB,YAAY,EAAE,MAAM;AAAA,IACpD;AAEA,UAAM,KAAK;AAAA,EACb;AACF;","names":[]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hono/csrf.ts"],"sourcesContent":["import type { MiddlewareHandler } from 'hono';\nimport { timingSafeEqual } from 'crypto';\nimport { getCookie } from 'hono/cookie';\nimport { METHOD_NAME_ALL } from 'hono/router';\nimport { RegExpRouter } from 'hono/router/reg-exp-router';\nimport { SmartRouter } from 'hono/router/smart-router';\nimport { TrieRouter } from 'hono/router/trie-router';\nimport { Status } from '../error/status';\n\ntype HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';\n\nexport type CSRFIgnoreRule = string | { path: string; methods?: [HTTPMethod, ...HTTPMethod[]] };\n\nexport interface CSRFConfig {\n /**\n * Cookie name for CSRF token\n * @default 'XSRF-TOKEN'\n */\n cookieName?: string;\n\n /**\n * Header name for CSRF token\n * @default 'X-XSRF-TOKEN'\n */\n headerName?: string;\n\n /**\n * Ignore rules for specific paths and methods\n * @example\n * [\n * { path: '/api/webhook/*', methods: ['POST'] },\n * { path: '/auth/apple/callback' }, // ignores all methods\n * ]\n */\n ignores?: CSRFIgnoreRule[];\n\n /**\n * Skip CSRF check for these methods\n * @default ['GET', 'HEAD', 'OPTIONS']\n */\n safeMethods?: HTTPMethod[];\n\n /**\n * Origin allowed to bypass CSRF check\n * @default undefined\n */\n origin?: string[];\n\n /**\n * Sec-Fetch-Site allowed to bypass CSRF check\n * @default undefined\n */\n secFetchSite?: Array<'same-origin' | 'same-site' | 'none' | 'cross-origin'>;\n\n /**\n * Custom error message\n * @default 'CSRF token validation failed'\n */\n errorMessage?: string;\n}\n\n/** use timing safe compare to prevent timing attack */\nfunction safeCompare(a: string, b: string): boolean {\n if (typeof a !== 'string' || typeof b !== 'string') return false;\n if (a.length === 0 || b.length === 0) return false;\n\n const bufferA = Buffer.from(a, 'utf-8');\n const bufferB = Buffer.from(b, 'utf-8');\n if (bufferA.length !== bufferB.length) return false;\n return timingSafeEqual(bufferA, bufferB);\n}\n\n/**\n * Create CSRF protection middleware\n *\n * @example\n * ```ts\n * import { Hono } from 'hono';\n * import { csrf } from '@shware/http/hono';\n *\n * const app = new Hono();\n *\n * // basic usage\n * app.use(csrf());\n *\n * // with configuration\n * app.use(csrf({\n * cookieName: 'csrf-token',\n * headerName: 'X-CSRF-Token',\n * ignores: [\n * { path: '/api/webhook/*', methods: ['POST'] },\n * { path: '/auth/apple/callback' },\n * ]\n * }));\n * ```\n */\nexport function csrf(config: CSRFConfig = {}): MiddlewareHandler {\n const cookieName = config.cookieName ?? 'XSRF-TOKEN';\n const headerName = config.headerName ?? 'X-XSRF-TOKEN';\n const safeMethods = new Set(config.safeMethods ?? ['GET', 'HEAD', 'OPTIONS']);\n const errorMessage = config.errorMessage ?? 'CSRF token validation failed';\n\n // initialize router for matching ignore rules\n const router = new SmartRouter<boolean>({\n routers: [new RegExpRouter(), new TrieRouter()],\n });\n\n // register ignore rules\n if (config.ignores) {\n for (const rule of config.ignores) {\n if (typeof rule === 'string') {\n router.add(METHOD_NAME_ALL, rule, true);\n } else if (rule.methods && rule.methods.length > 0) {\n for (const method of rule.methods) {\n router.add(method, rule.path, true);\n }\n } else {\n // if no methods are specified, ignore all methods\n router.add(METHOD_NAME_ALL, rule.path, true);\n }\n }\n }\n\n // return middleware\n return async (c, next) => {\n const method = c.req.method;\n const path = c.req.path;\n\n // check if the request should be ignored\n // 1. ignore safe methods\n if (safeMethods.has(method)) {\n await next();\n return;\n }\n\n // 2. ignore configured origin\n if (config.origin && config.origin.includes(c.req.header('origin') ?? '')) {\n await next();\n return;\n }\n\n // 3. ignore configured secFetchSite\n if (\n config.secFetchSite &&\n config.secFetchSite.includes((c.req.header('sec-fetch-site') ?? '') as never)\n ) {\n await next();\n return;\n }\n\n // 4. ignore configured ignore rules\n const [matched] = router.match(method, path);\n if (matched.length > 0) {\n await next();\n return;\n }\n\n const cookieToken = getCookie(c, cookieName);\n const headerToken = c.req.header(headerName);\n\n if (!cookieToken || !headerToken) {\n throw Status.permissionDenied(errorMessage).error();\n }\n\n if (!safeCompare(cookieToken, headerToken)) {\n throw Status.permissionDenied(errorMessage).error();\n }\n\n await next();\n };\n}\n"],"mappings":";AACA,SAAS,uBAAuB;AAChC,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;AAChC,SAAS,oBAAoB;AAC7B,SAAS,mBAAmB;AAC5B,SAAS,kBAAkB;AAC3B,SAAS,cAAc;AAuDvB,SAAS,YAAY,GAAW,GAAoB;AAClD,MAAI,OAAO,MAAM,YAAY,OAAO,MAAM,SAAU,QAAO;AAC3D,MAAI,EAAE,WAAW,KAAK,EAAE,WAAW,EAAG,QAAO;AAE7C,QAAM,UAAU,OAAO,KAAK,GAAG,OAAO;AACtC,QAAM,UAAU,OAAO,KAAK,GAAG,OAAO;AACtC,MAAI,QAAQ,WAAW,QAAQ,OAAQ,QAAO;AAC9C,SAAO,gBAAgB,SAAS,OAAO;AACzC;AA0BO,SAAS,KAAK,SAAqB,CAAC,GAAsB;AAC/D,QAAM,aAAa,OAAO,cAAc;AACxC,QAAM,aAAa,OAAO,cAAc;AACxC,QAAM,cAAc,IAAI,IAAI,OAAO,eAAe,CAAC,OAAO,QAAQ,SAAS,CAAC;AAC5E,QAAM,eAAe,OAAO,gBAAgB;AAG5C,QAAM,SAAS,IAAI,YAAqB;AAAA,IACtC,SAAS,CAAC,IAAI,aAAa,GAAG,IAAI,WAAW,CAAC;AAAA,EAChD,CAAC;AAGD,MAAI,OAAO,SAAS;AAClB,eAAW,QAAQ,OAAO,SAAS;AACjC,UAAI,OAAO,SAAS,UAAU;AAC5B,eAAO,IAAI,iBAAiB,MAAM,IAAI;AAAA,MACxC,WAAW,KAAK,WAAW,KAAK,QAAQ,SAAS,GAAG;AAClD,mBAAW,UAAU,KAAK,SAAS;AACjC,iBAAO,IAAI,QAAQ,KAAK,MAAM,IAAI;AAAA,QACpC;AAAA,MACF,OAAO;AAEL,eAAO,IAAI,iBAAiB,KAAK,MAAM,IAAI;AAAA,MAC7C;AAAA,IACF;AAAA,EACF;AAGA,SAAO,OAAO,GAAG,SAAS;AACxB,UAAM,SAAS,EAAE,IAAI;AACrB,UAAM,OAAO,EAAE,IAAI;AAInB,QAAI,YAAY,IAAI,MAAM,GAAG;AAC3B,YAAM,KAAK;AACX;AAAA,IACF;AAGA,QAAI,OAAO,UAAU,OAAO,OAAO,SAAS,EAAE,IAAI,OAAO,QAAQ,KAAK,EAAE,GAAG;AACzE,YAAM,KAAK;AACX;AAAA,IACF;AAGA,QACE,OAAO,gBACP,OAAO,aAAa,SAAU,EAAE,IAAI,OAAO,gBAAgB,KAAK,EAAY,GAC5E;AACA,YAAM,KAAK;AACX;AAAA,IACF;AAGA,UAAM,CAAC,OAAO,IAAI,OAAO,MAAM,QAAQ,IAAI;AAC3C,QAAI,QAAQ,SAAS,GAAG;AACtB,YAAM,KAAK;AACX;AAAA,IACF;AAEA,UAAM,cAAc,UAAU,GAAG,UAAU;AAC3C,UAAM,cAAc,EAAE,IAAI,OAAO,UAAU;AAE3C,QAAI,CAAC,eAAe,CAAC,aAAa;AAChC,YAAM,OAAO,iBAAiB,YAAY,EAAE,MAAM;AAAA,IACpD;AAEA,QAAI,CAAC,YAAY,aAAa,WAAW,GAAG;AAC1C,YAAM,OAAO,iBAAiB,YAAY,EAAE,MAAM;AAAA,IACpD;AAEA,UAAM,KAAK;AAAA,EACb;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/hono/csrf.ts"],"sourcesContent":["import { timingSafeEqual } from 'crypto';\nimport type { MiddlewareHandler } from 'hono';\nimport { getCookie } from 'hono/cookie';\nimport { METHOD_NAME_ALL } from 'hono/router';\nimport { RegExpRouter } from 'hono/router/reg-exp-router';\nimport { SmartRouter } from 'hono/router/smart-router';\nimport { TrieRouter } from 'hono/router/trie-router';\nimport { Status } from '../error/status';\n\ntype HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';\n\nexport type CSRFIgnoreRule = string | { path: string; methods?: [HTTPMethod, ...HTTPMethod[]] };\n\nexport interface CSRFConfig {\n /**\n * Cookie name for CSRF token\n * @default 'XSRF-TOKEN'\n */\n cookieName?: string;\n\n /**\n * Header name for CSRF token\n * @default 'X-XSRF-TOKEN'\n */\n headerName?: string;\n\n /**\n * Ignore rules for specific paths and methods\n * @example\n * [\n * { path: '/api/webhook/*', methods: ['POST'] },\n * { path: '/auth/apple/callback' }, // ignores all methods\n * ]\n */\n ignores?: CSRFIgnoreRule[];\n\n /**\n * Skip CSRF check for these methods\n * @default ['GET', 'HEAD', 'OPTIONS']\n */\n safeMethods?: HTTPMethod[];\n\n /**\n * Origin allowed to bypass CSRF check\n * @default undefined\n */\n origin?: string[];\n\n /**\n * Sec-Fetch-Site allowed to bypass CSRF check\n * @default undefined\n */\n secFetchSite?: Array<'same-origin' | 'same-site' | 'none' | 'cross-origin'>;\n\n /**\n * Custom error message\n * @default 'CSRF token validation failed'\n */\n errorMessage?: string;\n}\n\n/** use timing safe compare to prevent timing attack */\nfunction safeCompare(a: string, b: string): boolean {\n if (typeof a !== 'string' || typeof b !== 'string') return false;\n if (a.length === 0 || b.length === 0) return false;\n\n const bufferA = Buffer.from(a, 'utf-8');\n const bufferB = Buffer.from(b, 'utf-8');\n if (bufferA.length !== bufferB.length) return false;\n return timingSafeEqual(bufferA, bufferB);\n}\n\n/**\n * Create CSRF protection middleware\n *\n * @example\n * ```ts\n * import { Hono } from 'hono';\n * import { csrf } from '@shware/http/hono';\n *\n * const app = new Hono();\n *\n * // basic usage\n * app.use(csrf());\n *\n * // with configuration\n * app.use(csrf({\n * cookieName: 'csrf-token',\n * headerName: 'X-CSRF-Token',\n * ignores: [\n * { path: '/api/webhook/*', methods: ['POST'] },\n * { path: '/auth/apple/callback' },\n * ]\n * }));\n * ```\n */\nexport function csrf(config: CSRFConfig = {}): MiddlewareHandler {\n const cookieName = config.cookieName ?? 'XSRF-TOKEN';\n const headerName = config.headerName ?? 'X-XSRF-TOKEN';\n const safeMethods = new Set(config.safeMethods ?? ['GET', 'HEAD', 'OPTIONS']);\n const errorMessage = config.errorMessage ?? 'CSRF token validation failed';\n\n // initialize router for matching ignore rules\n const router = new SmartRouter<boolean>({\n routers: [new RegExpRouter(), new TrieRouter()],\n });\n\n // register ignore rules\n if (config.ignores) {\n for (const rule of config.ignores) {\n if (typeof rule === 'string') {\n router.add(METHOD_NAME_ALL, rule, true);\n } else if (rule.methods && rule.methods.length > 0) {\n for (const method of rule.methods) {\n router.add(method, rule.path, true);\n }\n } else {\n // if no methods are specified, ignore all methods\n router.add(METHOD_NAME_ALL, rule.path, true);\n }\n }\n }\n\n // return middleware\n return async (c, next) => {\n const method = c.req.method;\n const path = c.req.path;\n\n // check if the request should be ignored\n // 1. ignore safe methods\n if (safeMethods.has(method)) {\n await next();\n return;\n }\n\n // 2. ignore configured origin\n if (config.origin && config.origin.includes(c.req.header('origin') ?? '')) {\n await next();\n return;\n }\n\n // 3. ignore configured secFetchSite\n if (\n config.secFetchSite &&\n config.secFetchSite.includes((c.req.header('sec-fetch-site') ?? '') as never)\n ) {\n await next();\n return;\n }\n\n // 4. ignore configured ignore rules\n const [matched] = router.match(method, path);\n if (matched.length > 0) {\n await next();\n return;\n }\n\n const cookieToken = getCookie(c, cookieName);\n const headerToken = c.req.header(headerName);\n\n if (!cookieToken || !headerToken) {\n throw Status.permissionDenied(errorMessage).error();\n }\n\n if (!safeCompare(cookieToken, headerToken)) {\n throw Status.permissionDenied(errorMessage).error();\n }\n\n await next();\n };\n}\n"],"mappings":";AAAA,SAAS,uBAAuB;AAEhC,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;AAChC,SAAS,oBAAoB;AAC7B,SAAS,mBAAmB;AAC5B,SAAS,kBAAkB;AAC3B,SAAS,cAAc;AAuDvB,SAAS,YAAY,GAAW,GAAoB;AAClD,MAAI,OAAO,MAAM,YAAY,OAAO,MAAM,SAAU,QAAO;AAC3D,MAAI,EAAE,WAAW,KAAK,EAAE,WAAW,EAAG,QAAO;AAE7C,QAAM,UAAU,OAAO,KAAK,GAAG,OAAO;AACtC,QAAM,UAAU,OAAO,KAAK,GAAG,OAAO;AACtC,MAAI,QAAQ,WAAW,QAAQ,OAAQ,QAAO;AAC9C,SAAO,gBAAgB,SAAS,OAAO;AACzC;AA0BO,SAAS,KAAK,SAAqB,CAAC,GAAsB;AAC/D,QAAM,aAAa,OAAO,cAAc;AACxC,QAAM,aAAa,OAAO,cAAc;AACxC,QAAM,cAAc,IAAI,IAAI,OAAO,eAAe,CAAC,OAAO,QAAQ,SAAS,CAAC;AAC5E,QAAM,eAAe,OAAO,gBAAgB;AAG5C,QAAM,SAAS,IAAI,YAAqB;AAAA,IACtC,SAAS,CAAC,IAAI,aAAa,GAAG,IAAI,WAAW,CAAC;AAAA,EAChD,CAAC;AAGD,MAAI,OAAO,SAAS;AAClB,eAAW,QAAQ,OAAO,SAAS;AACjC,UAAI,OAAO,SAAS,UAAU;AAC5B,eAAO,IAAI,iBAAiB,MAAM,IAAI;AAAA,MACxC,WAAW,KAAK,WAAW,KAAK,QAAQ,SAAS,GAAG;AAClD,mBAAW,UAAU,KAAK,SAAS;AACjC,iBAAO,IAAI,QAAQ,KAAK,MAAM,IAAI;AAAA,QACpC;AAAA,MACF,OAAO;AAEL,eAAO,IAAI,iBAAiB,KAAK,MAAM,IAAI;AAAA,MAC7C;AAAA,IACF;AAAA,EACF;AAGA,SAAO,OAAO,GAAG,SAAS;AACxB,UAAM,SAAS,EAAE,IAAI;AACrB,UAAM,OAAO,EAAE,IAAI;AAInB,QAAI,YAAY,IAAI,MAAM,GAAG;AAC3B,YAAM,KAAK;AACX;AAAA,IACF;AAGA,QAAI,OAAO,UAAU,OAAO,OAAO,SAAS,EAAE,IAAI,OAAO,QAAQ,KAAK,EAAE,GAAG;AACzE,YAAM,KAAK;AACX;AAAA,IACF;AAGA,QACE,OAAO,gBACP,OAAO,aAAa,SAAU,EAAE,IAAI,OAAO,gBAAgB,KAAK,EAAY,GAC5E;AACA,YAAM,KAAK;AACX;AAAA,IACF;AAGA,UAAM,CAAC,OAAO,IAAI,OAAO,MAAM,QAAQ,IAAI;AAC3C,QAAI,QAAQ,SAAS,GAAG;AACtB,YAAM,KAAK;AACX;AAAA,IACF;AAEA,UAAM,cAAc,UAAU,GAAG,UAAU;AAC3C,UAAM,cAAc,EAAE,IAAI,OAAO,UAAU;AAE3C,QAAI,CAAC,eAAe,CAAC,aAAa;AAChC,YAAM,OAAO,iBAAiB,YAAY,EAAE,MAAM;AAAA,IACpD;AAEA,QAAI,CAAC,YAAY,aAAa,WAAW,GAAG;AAC1C,YAAM,OAAO,iBAAiB,YAAY,EAAE,MAAM;AAAA,IACpD;AAEA,UAAM,KAAK;AAAA,EACb;AACF;","names":[]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hono/validator.ts"],"sourcesContent":["import type { ValidationTargets } from 'hono';\nimport type { ZodType, output as outputV4 } from 'zod';\nimport { validator } from 'hono/validator';\nimport {\n NEVER,\n type ZodMiniType,\n type output as outputMini,\n pipe,\n string,\n transform,\n} from 'zod/mini';\nimport { type BadRequest, Details } from '../error/detail';\nimport { Status } from '../error/status';\n\nexport function zValidator<S extends ZodType | ZodMiniType>(\n target: keyof ValidationTargets,\n schema: S\n) {\n return validator(target, async (value) => {\n const result = await schema.safeParseAsync(value);\n if (result.success) return result.data as S extends ZodType ? outputV4<S> : outputMini<S>;\n\n const fieldViolations: BadRequest['fieldViolations'] = result.error.issues.map(\n ({ code, path, message }) => ({\n field: path.join('.'),\n description: message,\n reason: code?.toUpperCase() ?? 'INVALID_ARGUMENT',\n localizedMessage: { locale: 'en-US', message: message },\n })\n );\n const details = Details.new().badRequest({ fieldViolations });\n throw Status.invalidArgument().error(details);\n });\n}\n\nexport const bigintId = pipe(\n string(),\n transform((input, ctx) => {\n if (!/^(0|[1-9]\\d{0,19})$/.test(input)) {\n const message = `Invalid bigint id: ${input}`;\n ctx.issues.push({ code: 'custom', input, message });\n return NEVER;\n }\n try {\n return BigInt(input);\n } catch {\n const message = `Parse bigint id: ${input} failed`;\n ctx.issues.push({ code: 'custom', input, message });\n return NEVER;\n }\n })\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,uBAA0B;AAC1B,kBAOO;AACP,oBAAyC;AACzC,oBAAuB;AAEhB,SAAS,WACd,QACA,QACA;AACA,aAAO,4BAAU,QAAQ,OAAO,UAAU;AACxC,UAAM,SAAS,MAAM,OAAO,eAAe,KAAK;AAChD,QAAI,OAAO,QAAS,QAAO,OAAO;AAElC,UAAM,kBAAiD,OAAO,MAAM,OAAO;AAAA,MACzE,CAAC,EAAE,MAAM,MAAM,QAAQ,OAAO;AAAA,QAC5B,OAAO,KAAK,KAAK,GAAG;AAAA,QACpB,aAAa;AAAA,QACb,QAAQ,MAAM,YAAY,KAAK;AAAA,QAC/B,kBAAkB,EAAE,QAAQ,SAAS,QAAiB;AAAA,MACxD;AAAA,IACF;AACA,UAAM,UAAU,sBAAQ,IAAI,EAAE,WAAW,EAAE,gBAAgB,CAAC;AAC5D,UAAM,qBAAO,gBAAgB,EAAE,MAAM,OAAO;AAAA,EAC9C,CAAC;AACH;AAEO,IAAM,eAAW;AAAA,MACtB,oBAAO;AAAA,MACP,uBAAU,CAAC,OAAO,QAAQ;AACxB,QAAI,CAAC,sBAAsB,KAAK,KAAK,GAAG;AACtC,YAAM,UAAU,sBAAsB,KAAK;AAC3C,UAAI,OAAO,KAAK,EAAE,MAAM,UAAU,OAAO,QAAQ,CAAC;AAClD,aAAO;AAAA,IACT;AACA,QAAI;AACF,aAAO,OAAO,KAAK;AAAA,IACrB,QAAQ;AACN,YAAM,UAAU,oBAAoB,KAAK;AACzC,UAAI,OAAO,KAAK,EAAE,MAAM,UAAU,OAAO,QAAQ,CAAC;AAClD,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AACH;","names":[]}
1
+ {"version":3,"sources":["../../src/hono/validator.ts"],"sourcesContent":["import type { ValidationTargets } from 'hono';\nimport { validator } from 'hono/validator';\nimport type { ZodType, output as outputV4 } from 'zod';\nimport {\n NEVER,\n type ZodMiniType,\n type output as outputMini,\n pipe,\n string,\n transform,\n} from 'zod/mini';\nimport { type BadRequest, Details } from '../error/detail';\nimport { Status } from '../error/status';\n\nexport function zValidator<S extends ZodType | ZodMiniType>(\n target: keyof ValidationTargets,\n schema: S\n) {\n return validator(target, async (value) => {\n const result = await schema.safeParseAsync(value);\n if (result.success) return result.data as S extends ZodType ? outputV4<S> : outputMini<S>;\n\n const fieldViolations: BadRequest['fieldViolations'] = result.error.issues.map(\n ({ code, path, message }) => ({\n field: path.join('.'),\n description: message,\n reason: code?.toUpperCase() ?? 'INVALID_ARGUMENT',\n localizedMessage: { locale: 'en-US', message: message },\n })\n );\n const details = Details.new().badRequest({ fieldViolations });\n throw Status.invalidArgument().error(details);\n });\n}\n\nexport const bigintId = pipe(\n string(),\n transform((input, ctx) => {\n if (!/^(0|[1-9]\\d{0,19})$/.test(input)) {\n const message = `Invalid bigint id: ${input}`;\n ctx.issues.push({ code: 'custom', input, message });\n return NEVER;\n }\n try {\n return BigInt(input);\n } catch {\n const message = `Parse bigint id: ${input} failed`;\n ctx.issues.push({ code: 'custom', input, message });\n return NEVER;\n }\n })\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,uBAA0B;AAE1B,kBAOO;AACP,oBAAyC;AACzC,oBAAuB;AAEhB,SAAS,WACd,QACA,QACA;AACA,aAAO,4BAAU,QAAQ,OAAO,UAAU;AACxC,UAAM,SAAS,MAAM,OAAO,eAAe,KAAK;AAChD,QAAI,OAAO,QAAS,QAAO,OAAO;AAElC,UAAM,kBAAiD,OAAO,MAAM,OAAO;AAAA,MACzE,CAAC,EAAE,MAAM,MAAM,QAAQ,OAAO;AAAA,QAC5B,OAAO,KAAK,KAAK,GAAG;AAAA,QACpB,aAAa;AAAA,QACb,QAAQ,MAAM,YAAY,KAAK;AAAA,QAC/B,kBAAkB,EAAE,QAAQ,SAAS,QAAiB;AAAA,MACxD;AAAA,IACF;AACA,UAAM,UAAU,sBAAQ,IAAI,EAAE,WAAW,EAAE,gBAAgB,CAAC;AAC5D,UAAM,qBAAO,gBAAgB,EAAE,MAAM,OAAO;AAAA,EAC9C,CAAC;AACH;AAEO,IAAM,eAAW;AAAA,MACtB,oBAAO;AAAA,MACP,uBAAU,CAAC,OAAO,QAAQ;AACxB,QAAI,CAAC,sBAAsB,KAAK,KAAK,GAAG;AACtC,YAAM,UAAU,sBAAsB,KAAK;AAC3C,UAAI,OAAO,KAAK,EAAE,MAAM,UAAU,OAAO,QAAQ,CAAC;AAClD,aAAO;AAAA,IACT;AACA,QAAI;AACF,aAAO,OAAO,KAAK;AAAA,IACrB,QAAQ;AACN,YAAM,UAAU,oBAAoB,KAAK;AACzC,UAAI,OAAO,KAAK,EAAE,MAAM,UAAU,OAAO,QAAQ,CAAC;AAClD,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AACH;","names":[]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hono/validator.ts"],"sourcesContent":["import type { ValidationTargets } from 'hono';\nimport type { ZodType, output as outputV4 } from 'zod';\nimport { validator } from 'hono/validator';\nimport {\n NEVER,\n type ZodMiniType,\n type output as outputMini,\n pipe,\n string,\n transform,\n} from 'zod/mini';\nimport { type BadRequest, Details } from '../error/detail';\nimport { Status } from '../error/status';\n\nexport function zValidator<S extends ZodType | ZodMiniType>(\n target: keyof ValidationTargets,\n schema: S\n) {\n return validator(target, async (value) => {\n const result = await schema.safeParseAsync(value);\n if (result.success) return result.data as S extends ZodType ? outputV4<S> : outputMini<S>;\n\n const fieldViolations: BadRequest['fieldViolations'] = result.error.issues.map(\n ({ code, path, message }) => ({\n field: path.join('.'),\n description: message,\n reason: code?.toUpperCase() ?? 'INVALID_ARGUMENT',\n localizedMessage: { locale: 'en-US', message: message },\n })\n );\n const details = Details.new().badRequest({ fieldViolations });\n throw Status.invalidArgument().error(details);\n });\n}\n\nexport const bigintId = pipe(\n string(),\n transform((input, ctx) => {\n if (!/^(0|[1-9]\\d{0,19})$/.test(input)) {\n const message = `Invalid bigint id: ${input}`;\n ctx.issues.push({ code: 'custom', input, message });\n return NEVER;\n }\n try {\n return BigInt(input);\n } catch {\n const message = `Parse bigint id: ${input} failed`;\n ctx.issues.push({ code: 'custom', input, message });\n return NEVER;\n }\n })\n);\n"],"mappings":";AAEA,SAAS,iBAAiB;AAC1B;AAAA,EACE;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAA0B,eAAe;AACzC,SAAS,cAAc;AAEhB,SAAS,WACd,QACA,QACA;AACA,SAAO,UAAU,QAAQ,OAAO,UAAU;AACxC,UAAM,SAAS,MAAM,OAAO,eAAe,KAAK;AAChD,QAAI,OAAO,QAAS,QAAO,OAAO;AAElC,UAAM,kBAAiD,OAAO,MAAM,OAAO;AAAA,MACzE,CAAC,EAAE,MAAM,MAAM,QAAQ,OAAO;AAAA,QAC5B,OAAO,KAAK,KAAK,GAAG;AAAA,QACpB,aAAa;AAAA,QACb,QAAQ,MAAM,YAAY,KAAK;AAAA,QAC/B,kBAAkB,EAAE,QAAQ,SAAS,QAAiB;AAAA,MACxD;AAAA,IACF;AACA,UAAM,UAAU,QAAQ,IAAI,EAAE,WAAW,EAAE,gBAAgB,CAAC;AAC5D,UAAM,OAAO,gBAAgB,EAAE,MAAM,OAAO;AAAA,EAC9C,CAAC;AACH;AAEO,IAAM,WAAW;AAAA,EACtB,OAAO;AAAA,EACP,UAAU,CAAC,OAAO,QAAQ;AACxB,QAAI,CAAC,sBAAsB,KAAK,KAAK,GAAG;AACtC,YAAM,UAAU,sBAAsB,KAAK;AAC3C,UAAI,OAAO,KAAK,EAAE,MAAM,UAAU,OAAO,QAAQ,CAAC;AAClD,aAAO;AAAA,IACT;AACA,QAAI;AACF,aAAO,OAAO,KAAK;AAAA,IACrB,QAAQ;AACN,YAAM,UAAU,oBAAoB,KAAK;AACzC,UAAI,OAAO,KAAK,EAAE,MAAM,UAAU,OAAO,QAAQ,CAAC;AAClD,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AACH;","names":[]}
1
+ {"version":3,"sources":["../../src/hono/validator.ts"],"sourcesContent":["import type { ValidationTargets } from 'hono';\nimport { validator } from 'hono/validator';\nimport type { ZodType, output as outputV4 } from 'zod';\nimport {\n NEVER,\n type ZodMiniType,\n type output as outputMini,\n pipe,\n string,\n transform,\n} from 'zod/mini';\nimport { type BadRequest, Details } from '../error/detail';\nimport { Status } from '../error/status';\n\nexport function zValidator<S extends ZodType | ZodMiniType>(\n target: keyof ValidationTargets,\n schema: S\n) {\n return validator(target, async (value) => {\n const result = await schema.safeParseAsync(value);\n if (result.success) return result.data as S extends ZodType ? outputV4<S> : outputMini<S>;\n\n const fieldViolations: BadRequest['fieldViolations'] = result.error.issues.map(\n ({ code, path, message }) => ({\n field: path.join('.'),\n description: message,\n reason: code?.toUpperCase() ?? 'INVALID_ARGUMENT',\n localizedMessage: { locale: 'en-US', message: message },\n })\n );\n const details = Details.new().badRequest({ fieldViolations });\n throw Status.invalidArgument().error(details);\n });\n}\n\nexport const bigintId = pipe(\n string(),\n transform((input, ctx) => {\n if (!/^(0|[1-9]\\d{0,19})$/.test(input)) {\n const message = `Invalid bigint id: ${input}`;\n ctx.issues.push({ code: 'custom', input, message });\n return NEVER;\n }\n try {\n return BigInt(input);\n } catch {\n const message = `Parse bigint id: ${input} failed`;\n ctx.issues.push({ code: 'custom', input, message });\n return NEVER;\n }\n })\n);\n"],"mappings":";AACA,SAAS,iBAAiB;AAE1B;AAAA,EACE;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAA0B,eAAe;AACzC,SAAS,cAAc;AAEhB,SAAS,WACd,QACA,QACA;AACA,SAAO,UAAU,QAAQ,OAAO,UAAU;AACxC,UAAM,SAAS,MAAM,OAAO,eAAe,KAAK;AAChD,QAAI,OAAO,QAAS,QAAO,OAAO;AAElC,UAAM,kBAAiD,OAAO,MAAM,OAAO;AAAA,MACzE,CAAC,EAAE,MAAM,MAAM,QAAQ,OAAO;AAAA,QAC5B,OAAO,KAAK,KAAK,GAAG;AAAA,QACpB,aAAa;AAAA,QACb,QAAQ,MAAM,YAAY,KAAK;AAAA,QAC/B,kBAAkB,EAAE,QAAQ,SAAS,QAAiB;AAAA,MACxD;AAAA,IACF;AACA,UAAM,UAAU,QAAQ,IAAI,EAAE,WAAW,EAAE,gBAAgB,CAAC;AAC5D,UAAM,OAAO,gBAAgB,EAAE,MAAM,OAAO;AAAA,EAC9C,CAAC;AACH;AAEO,IAAM,WAAW;AAAA,EACtB,OAAO;AAAA,EACP,UAAU,CAAC,OAAO,QAAQ;AACxB,QAAI,CAAC,sBAAsB,KAAK,KAAK,GAAG;AACtC,YAAM,UAAU,sBAAsB,KAAK;AAC3C,UAAI,OAAO,KAAK,EAAE,MAAM,UAAU,OAAO,QAAQ,CAAC;AAClD,aAAO;AAAA,IACT;AACA,QAAI;AACF,aAAO,OAAO,KAAK;AAAA,IACrB,QAAQ;AACN,YAAM,UAAU,oBAAoB,KAAK;AACzC,UAAI,OAAO,KAAK,EAAE,MAAM,UAAU,OAAO,QAAQ,CAAC;AAClD,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AACH;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shware/http",
3
- "version": "2.7.1",
3
+ "version": "2.9.0",
4
4
  "private": false,
5
5
  "repository": {
6
6
  "type": "git",
@@ -38,19 +38,19 @@
38
38
  }
39
39
  },
40
40
  "dependencies": {
41
- "vitest": "^4.0.17",
42
- "zod": "^4.3.5",
43
- "@shware/utils": "^1.1.5"
41
+ "vitest": "^4.0.18",
42
+ "zod": "^4.3.6",
43
+ "@shware/utils": "^1.2.0"
44
44
  },
45
45
  "devDependencies": {
46
- "@types/node": "^24",
47
- "@types/react": "^19.2.8",
46
+ "@types/node": "^25",
47
+ "@types/react": "^19.2.14",
48
48
  "typescript": "^5.9.3",
49
49
  "@repo/typescript-config": "0.0.0"
50
50
  },
51
51
  "peerDependencies": {
52
- "hono": "^4.10.4",
53
- "i18next": "^25.0.0",
52
+ "hono": "^4.12.2",
53
+ "i18next": "^25.8.13",
54
54
  "react": "^19"
55
55
  },
56
56
  "peerDependenciesMeta": {