intor 2.4.8 → 2.4.9
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/README.md +3 -8
- package/dist/next/src/adapters/next/create-intor-handler.js +37 -35
- package/dist/types/src/adapters/next/create-intor-handler.d.ts +2 -1
- package/dist/types/src/core/index.d.ts +1 -1
- package/dist/types/src/core/types/generated.d.ts +35 -31
- package/dist/types/src/core/types/index.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,11 +21,6 @@ The i18n library for modern JavaScript
|
|
|
21
21
|
</div>
|
|
22
22
|
|
|
23
23
|
> [!NOTE]
|
|
24
|
-
> Intor
|
|
25
|
-
>
|
|
26
|
-
>
|
|
27
|
-
> framework-agnostic translation system.
|
|
28
|
-
>
|
|
29
|
-
> **v3 marks its first intentional public release.**
|
|
30
|
-
>
|
|
31
|
-
> _Currently available as v2.x. v3 is in active development._
|
|
24
|
+
> Intor is evolving toward long-term stability.
|
|
25
|
+
> **v3 will be the first fully stable release.**
|
|
26
|
+
> _Current: v2.x · v3 in development._
|
|
@@ -17,40 +17,42 @@ import { getLocaleFromAcceptLanguage } from '../../routing/locale/get-locale-fro
|
|
|
17
17
|
*
|
|
18
18
|
* @platform Next.js
|
|
19
19
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
20
|
+
function createIntorHandler(config) {
|
|
21
|
+
return async function intorHandler(request) {
|
|
22
|
+
const { host, searchParams, pathname: rawPathname } = request.nextUrl;
|
|
23
|
+
// Locale from Accept-Language header
|
|
24
|
+
const acceptLanguageHeader = request.headers.get("accept-language");
|
|
25
|
+
const localeFromAcceptLanguage = getLocaleFromAcceptLanguage(acceptLanguageHeader, config.supportedLocales);
|
|
26
|
+
// Check whether this navigation flow has already redirected
|
|
27
|
+
const hasRedirected = request.headers.get(INTOR_HEADERS.REDIRECTED) === "1";
|
|
28
|
+
// ----------------------------------------------------------
|
|
29
|
+
// Resolve inbound routing decision (pure computation)
|
|
30
|
+
// ----------------------------------------------------------
|
|
31
|
+
const { locale, localeSource, pathname, shouldRedirect } = await resolveInbound(config, rawPathname, {
|
|
32
|
+
host,
|
|
33
|
+
query: normalizeQuery(Object.fromEntries(searchParams.entries())),
|
|
34
|
+
cookie: request.cookies.get(config.cookie.name)?.value,
|
|
35
|
+
detected: localeFromAcceptLanguage || config.defaultLocale,
|
|
36
|
+
}, { hasRedirected });
|
|
37
|
+
// ----------------------------------------------------------
|
|
38
|
+
// Prepare Next.js response (redirect or pass-through)
|
|
39
|
+
// ----------------------------------------------------------
|
|
40
|
+
const url = request.nextUrl.clone();
|
|
41
|
+
url.pathname = pathname;
|
|
42
|
+
const response = shouldRedirect
|
|
43
|
+
? NextResponse.redirect(url)
|
|
44
|
+
: NextResponse.next();
|
|
45
|
+
// ----------------------------------------------------------
|
|
46
|
+
// Attach routing metadata to response headers
|
|
47
|
+
// ----------------------------------------------------------
|
|
48
|
+
response.headers.set(INTOR_HEADERS.LOCALE, locale);
|
|
49
|
+
response.headers.set(INTOR_HEADERS.LOCALE_SOURCE, localeSource);
|
|
50
|
+
response.headers.set(INTOR_HEADERS.PATHNAME, pathname);
|
|
51
|
+
// Mark redirect to prevent infinite loops in this flow
|
|
52
|
+
if (shouldRedirect)
|
|
53
|
+
response.headers.set(INTOR_HEADERS.REDIRECTED, "1");
|
|
54
|
+
return response;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
55
57
|
|
|
56
58
|
export { createIntorHandler };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { IntorResolvedConfig } from "../../config";
|
|
2
2
|
import type { NextRequest } from "next/server";
|
|
3
|
+
import { NextResponse } from "next/server";
|
|
3
4
|
/**
|
|
4
5
|
* Resolves locale-aware routing for the current execution context.
|
|
5
6
|
*
|
|
@@ -9,4 +10,4 @@ import type { NextRequest } from "next/server";
|
|
|
9
10
|
*
|
|
10
11
|
* @platform Next.js
|
|
11
12
|
*/
|
|
12
|
-
export declare
|
|
13
|
+
export declare function createIntorHandler(config: IntorResolvedConfig): (request: NextRequest) => Promise<NextResponse<unknown>>;
|
|
@@ -5,4 +5,4 @@ export { getLogger, clearLoggerPool } from "./logger";
|
|
|
5
5
|
export { loadRemoteMessages, mergeMessages, isValidMessages, nestObjectFromPath, type MessagesReader, type MessagesReaders, INTOR_PREFIX, INTOR_MESSAGES_KIND_KEY, INTOR_MESSAGES_KIND, getMessagesKind, type IntorMessagesKind, } from "./messages";
|
|
6
6
|
export { createHtmlRenderer, type TagRenderers, type HtmlTagRenderers, } from "./render";
|
|
7
7
|
export { createTranslator, type CreateTranslatorParams, createTRich, } from "./translator";
|
|
8
|
-
export type { INTOR_GENERATED_KEY,
|
|
8
|
+
export type { INTOR_GENERATED_KEY, GenConfigKeys, GenConfig, GenMessages, GenLocale, GenReplacements, GenRich, TranslatorInstance, RoutingLocaleSource, RoutingLocaleCarrier, LocalePathPrefix, RuntimeFetch, } from "./types";
|
|
@@ -1,41 +1,44 @@
|
|
|
1
1
|
import type { PREFIX_PLACEHOLDER } from "../constants";
|
|
2
2
|
import type { Locale, LocaleMessages, Replacement, Rich } from "intor-translator";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
* Generated-aware type system for Intor
|
|
6
|
-
*
|
|
7
|
-
* This module defines conditional and fallback types that adapt
|
|
8
|
-
* based on whether generated types are present.
|
|
9
|
-
* ================================================
|
|
4
|
+
* =================================================
|
|
5
|
+
* Generated-aware type system for Intor
|
|
6
|
+
* =================================================
|
|
10
7
|
*/
|
|
11
8
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* - Type-level only. Not for runtime or user-facing usage.
|
|
9
|
+
* Sentinel key injected by CLI when generated types exist.
|
|
10
|
+
* Used purely for type-level generation detection.
|
|
15
11
|
*/
|
|
16
12
|
export type INTOR_GENERATED_KEY = "__intor_generated__";
|
|
17
13
|
/**
|
|
18
|
-
*
|
|
19
|
-
* - Uses key presence on `IntorGeneratedTypes` to detect generation.
|
|
14
|
+
* Detect whether generated types are present.
|
|
20
15
|
*/
|
|
21
|
-
|
|
16
|
+
type HasGenerated = IntorGeneratedTypes extends {
|
|
22
17
|
[K in INTOR_GENERATED_KEY]: true;
|
|
23
|
-
} ?
|
|
18
|
+
} ? true : false;
|
|
24
19
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
20
|
+
* Extract valid configuration keys from generated types.
|
|
21
|
+
* (Excludes sentinel key.)
|
|
27
22
|
*/
|
|
28
23
|
type GeneratedConfigKeys = Exclude<keyof IntorGeneratedTypes, INTOR_GENERATED_KEY>;
|
|
29
24
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
25
|
+
* Public configuration key union.
|
|
26
|
+
* Falls back to `string` in non-generated mode.
|
|
27
|
+
*/
|
|
28
|
+
export type GenConfigKeys = HasGenerated extends true ? GeneratedConfigKeys : string;
|
|
29
|
+
/**
|
|
30
|
+
* Fallback configuration shape (non-generated mode).
|
|
32
31
|
*/
|
|
33
|
-
|
|
32
|
+
type FallbackConfig = {
|
|
33
|
+
Locales: Locale;
|
|
34
|
+
Messages: LocaleMessages;
|
|
35
|
+
Replacements: Replacement;
|
|
36
|
+
Rich: Rich;
|
|
37
|
+
};
|
|
34
38
|
/**
|
|
35
|
-
*
|
|
36
|
-
* - If `IntorGeneratedTypes` is not defined, falls back to default shape.
|
|
39
|
+
* Extract generated configuration shape safely.
|
|
37
40
|
*/
|
|
38
|
-
|
|
41
|
+
type ExtractGeneratedConfig<T> = T extends {
|
|
39
42
|
Locales: infer L extends string;
|
|
40
43
|
Messages: Record<typeof PREFIX_PLACEHOLDER, infer M>;
|
|
41
44
|
Replacements: infer RE;
|
|
@@ -45,18 +48,19 @@ export type GenConfig<CK extends GenConfigKeys> = IfGen<CK extends keyof IntorGe
|
|
|
45
48
|
Messages: Record<L, M>;
|
|
46
49
|
Replacements: RE;
|
|
47
50
|
Rich: RI;
|
|
48
|
-
} : never
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
} : never;
|
|
52
|
+
/**
|
|
53
|
+
* Configuration shape resolver.
|
|
54
|
+
*
|
|
55
|
+
* - Uses generated types when available.
|
|
56
|
+
* - Falls back to default shape otherwise.
|
|
57
|
+
*/
|
|
58
|
+
export type GenConfig<CK extends GenConfigKeys> = HasGenerated extends true ? CK extends GeneratedConfigKeys ? ExtractGeneratedConfig<IntorGeneratedTypes[CK]> : never : FallbackConfig;
|
|
59
|
+
/**
|
|
60
|
+
* Derived helpers
|
|
61
|
+
*/
|
|
55
62
|
export type GenMessages<CK extends GenConfigKeys> = GenConfig<CK>["Messages"];
|
|
56
|
-
/** Resolves locale union for a given config key */
|
|
57
63
|
export type GenLocale<CK extends GenConfigKeys> = GenConfig<CK>["Locales"];
|
|
58
|
-
/** Resolves replacement schema for a given config key */
|
|
59
64
|
export type GenReplacements<CK extends GenConfigKeys> = GenConfig<CK>["Replacements"];
|
|
60
|
-
/** Resolves rich tag schema for a given config key */
|
|
61
65
|
export type GenRich<CK extends GenConfigKeys> = GenConfig<CK>["Rich"];
|
|
62
66
|
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { INTOR_GENERATED_KEY,
|
|
1
|
+
export type { INTOR_GENERATED_KEY, GenConfigKeys, GenConfig, GenMessages, GenLocale, GenReplacements, GenRich, } from "./generated";
|
|
2
2
|
export type { TranslatorInstance } from "./translator-instance";
|
|
3
3
|
export type { RoutingLocaleSource, RoutingLocaleCarrier, LocalePathPrefix, } from "./routing";
|
|
4
4
|
export type { RuntimeFetch } from "./runtime-fetch";
|