Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/37610-own-door-501-locale-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@objectstack/spec': patch
'@objectstack/runtime': patch
---

Clause-②: yes (widening)
Metadata-protocol: none
Skills-docs: none (no page under `skills/**` names these doors' `501` sentences)

The comment doors and the webhook redelivery door now answer their `501 NOT_IMPLEMENTED` in the request's language, as the print, personal-view and attachment doors do. On a kernel with no `comments` or `webhooks` service, or with one that lacks `handleCommentDoor` / `handleRedeliver`, a `zh-CN`, `ja-JP` or `es-ES` request read the sentence in English. The operation-message catalog (`BUILTIN_OPERATION_MESSAGES`, `@objectstack/spec/system`) gains `comment_doors_service_unregistered`, `comment_doors_member_missing`, `webhook_redeliver_service_unregistered` and `webhook_redeliver_member_missing` in `en`, `zh-CN`, `ja-JP` and `es-ES`; their English is the sentence each door answered before, byte for byte. Code and status do not change, and no `userMessage` is added.

**The English moved for one door.** The attachment removal and rename doors' `501` now reads "The attachment removal and rename doors **are** not available: …" (it read "is"). The catalog gains `own_door_service_unregistered_plural` and `own_door_member_missing_plural` in the four locales, read by a bridge whose noun is plural; the `es-ES` noun of `own_door_attachment_mutate` becomes plural to match ("Los puntos de acceso para eliminar y renombrar adjuntos no están disponibles: …"). The `zh-CN` and `ja-JP` sentences do not change.

In `@objectstack/runtime` (internal), the own-door bridge's `nounKey` is required, so no bridge can ship a `501` that stays English under another language.
2 changes: 2 additions & 0 deletions packages/runtime/src/domains/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const ATTACHMENTS_BRIDGE: OwnDoorBridge = Object.freeze({
noun: 'the attachment removal and rename doors',
// [#37352] The catalog key of `noun`: the bridge's `501` follows the request's language.
nounKey: 'own_door_attachment_mutate',
// [#37610] Two doors: the `501` says "are".
nounPlural: true,
tag: 'attachments',
});

Expand Down
53 changes: 53 additions & 0 deletions packages/runtime/src/domains/comments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,56 @@ describe('[#36469] no preamble-gate exemption — the credential is the session,
expect(comments.handleCommentDoor).not.toHaveBeenCalled();
});
});

// [#37610] The comment doors' two `501` sentences are the operation-message
// catalog's, in the request's top `Accept-Language` tag, as the own-door
// bridge answers its doors' own. The English is byte for byte the sentence the
// door answered before, the code and status are unchanged, and no
// `userMessage` is added.
describe('[#37610] the comment doors\' 501 follows the request\'s language', () => {
const SENTENCES = {
unregistered: {
en: 'The comment doors are not available: no comments service is registered. @objectstack/plugin-audit registers it; compose AuditPlugin on this kernel.',
'zh-CN': '评论接口不可用:未注册 comments 服务。该服务由 @objectstack/plugin-audit 注册;请在此内核上组合 AuditPlugin。',
'ja-JP': 'コメントエンドポイントは利用できません。comments サービスが登録されていません。このサービスは @objectstack/plugin-audit が登録します。このカーネルに AuditPlugin を組み込んでください。',
'es-ES': 'Los puntos de acceso de comentarios no están disponibles: no hay ningún servicio comments registrado. Lo registra @objectstack/plugin-audit; componga AuditPlugin en este kernel.',
},
memberMissing: {
en: 'The comment doors are not available: the registered comments service does not implement handleCommentDoor.',
'zh-CN': '评论接口不可用:已注册的 comments 服务未实现 handleCommentDoor。',
'ja-JP': 'コメントエンドポイントは利用できません。登録されている comments サービスは handleCommentDoor を実装していません。',
'es-ES': 'Los puntos de acceso de comentarios no están disponibles: el servicio comments registrado no implementa handleCommentDoor.',
},
};
const HEADERS: Record<keyof typeof SENTENCES.unregistered, Array<Record<string, string>>> = {
en: [{ 'accept-language': 'en-US,en;q=0.9' }, {}],
'zh-CN': [{ 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8' }],
'ja-JP': [{ 'accept-language': 'ja-JP,ja;q=0.9' }],
'es-ES': [{ 'accept-language': 'es-ES,es;q=0.9' }],
};

for (const site of [
{ name: 'no comments slot on the kernel', services: {} as Record<string, unknown>, sentences: SENTENCES.unregistered },
{ name: 'a comments service without handleCommentDoor', services: { comments: { somethingElse: vi.fn() } }, sentences: SENTENCES.memberMissing },
]) {
it(`${site.name}: 501 NOT_IMPLEMENTED in en (and with no header), zh-CN, ja-JP and es-ES, on every door`, async () => {
for (const [method, path, body] of DOORS) {
for (const [locale, list] of Object.entries(HEADERS) as Array<[keyof typeof HEADERS, Array<Record<string, string>>]>) {
for (const headers of list) {
const { dispatcher } = makeDispatcher(site.services);
const request = new Request(`${ORIGIN}/api/v1${path}`, {
method,
headers: { ...JSON_HEADERS, ...headers },
...(body === undefined ? {} : { body: JSON.stringify(body) }),
});
const label = `${method} ${path} ${JSON.stringify(headers)}`;
const result = await dispatcher.dispatch(method, path, {}, {}, ctx(request));
expect(envelope(result), label).toEqual({ status: 501, code: 'NOT_IMPLEMENTED' });
expect(result.response?.body?.error?.message, label).toBe(site.sentences[locale]);
expect(result.response?.body?.error?.userMessage, label).toBeUndefined();
}
}
}
});
}
});
10 changes: 6 additions & 4 deletions packages/runtime/src/domains/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@
*/

import { INTERNAL_ERROR_MESSAGE } from '@objectstack/types';
import { operationUserMessage } from '@objectstack/core';
import type { ICommentDoorsService } from '@objectstack/spec/contracts';
import type { HttpProtocolContext, HttpDispatcherResult } from '../http-dispatcher.js';
import type { DomainHandlerDeps, DomainRequest, DomainRoute } from '../domain-handler-registry.js';
import { requestLocaleOf } from './own-door-bridge.js';

/** The prefix the domain claims (a segment claim, narrowed by the handler). */
export const COMMENTS_DOMAIN_PREFIX = '/comments';
Expand Down Expand Up @@ -158,12 +160,13 @@ export async function handleCommentsRequest(

const service = await deps.resolveService(context, COMMENTS_SLOT, context.environmentId) as
Pick<ICommentDoorsService, 'handleCommentDoor'> | undefined;
// [#37610] The `501` is the operation-message catalog's, in the request's
// top `Accept-Language` tag, as the own-door bridge answers its own.
if (!service) {
return {
handled: true,
response: deps.error(
'The comment doors are not available: no comments service is registered. '
+ '@objectstack/plugin-audit registers it; compose AuditPlugin on this kernel.',
operationUserMessage('comment_doors_service_unregistered', { locale: requestLocaleOf(context) }),
501,
),
};
Expand All @@ -172,8 +175,7 @@ export async function handleCommentsRequest(
return {
handled: true,
response: deps.error(
'The comment doors are not available: the registered comments service '
+ 'does not implement handleCommentDoor.',
operationUserMessage('comment_doors_member_missing', { locale: requestLocaleOf(context) }),
501,
),
};
Expand Down
40 changes: 10 additions & 30 deletions packages/runtime/src/domains/own-door-bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import '@objectstack/metadata';
import { HttpDispatcher } from '../http-dispatcher.js';
import type { HttpProtocolContext, KernelResolver } from '../http-dispatcher.js';
import { BUILTIN_OPERATION_MESSAGES } from '@objectstack/spec/system';
import { handleOwnDoorBridgeRequest } from './own-door-bridge.js';
import { PERSONAL_VIEWS_BRIDGE } from './personal-views.js';
import { ATTACHMENTS_BRIDGE } from './attachments.js';
import { PRINT_BRIDGE } from './print.js';
Expand Down Expand Up @@ -377,7 +376,7 @@ describe.each(FAMILIES)('[#36506] $prefix forwards its doors to the $slot member
// [#37273] The bridge's two `501` sentences are the operation-message
// catalog's, in the request's top `Accept-Language` tag, for a bridge that
// names its noun's key (the print door). Code and status are unchanged, no
// `userMessage` is added, and a bridge that names no noun key keeps English.
// `userMessage` is added.
describe('[#37273] the print door\'s 501 follows the request\'s language', () => {
const [method, path, body] = ['POST', '/print/records', { app: 'sales', object: 'invoice', recordId: 'inv_1', page: 'invoice_document' }] as const;
const withHeaders = (headers: Record<string, string>) => {
Expand Down Expand Up @@ -416,27 +415,8 @@ describe('[#37273] the print door\'s 501 follows the request\'s language', () =>
}
});

it('a bridge that names no noun key keeps its English sentence under zh-CN: never half translated', async () => {
// [#37352] Every shipped bridge names its noun key now, so the control is
// a shipped bridge with the key taken off, handed to the bridge directly.
const deps = {
routeNotFound: vi.fn(),
getRequestKernelService: async () => undefined,
error: (message: string, status: number) => ({ status, body: { error: { code: 'NOT_IMPLEMENTED', message } } }),
} as any;
const request = new Request(`${ORIGIN}/api/v1/personal-views/pv_1`, { method: 'DELETE', headers: { ...JSON_HEADERS, 'accept-language': 'zh-CN' } });
const result = await handleOwnDoorBridgeRequest(
deps,
{ ...PERSONAL_VIEWS_BRIDGE, nounKey: undefined },
{ method: 'DELETE', path: '/personal-views/pv_1', body: {}, query: {} },
ctx(request),
);
expect(envelope(result)).toEqual({ status: 501, code: 'NOT_IMPLEMENTED' });
expect(result.response?.body?.error?.message).toBe(
'The personal view delete door is not available: no personal-views service is registered on this kernel. '
+ '@objectstack/plugin-auth (AuthPlugin) registers it.',
);
});
// [#37610] The English-only arm is gone: `nounKey` is required, so no
// bridge can ship a `501` that stays English under another language.
});

// [#37352] The personal-view and attachment doors sit on the same bridge as the
Expand Down Expand Up @@ -469,16 +449,16 @@ describe('[#37352] the personal-view and attachment doors\' 501s follow the requ
...AT,
requests: [['DELETE', '/attachments/att_1'], ['PATCH', '/attachments/att_1', { file_name: 'renamed.pdf' }]] as Array<[string, string, unknown?]>,
unregistered: {
en: 'The attachment removal and rename doors is not available: no file-attachments service is registered on this kernel. @objectstack/service-storage (StorageServicePlugin) registers it.',
en: 'The attachment removal and rename doors are not available: no file-attachments service is registered on this kernel. @objectstack/service-storage (StorageServicePlugin) registers it.',
'zh-CN': '附件删除和重命名接口不可用:此内核上未注册 file-attachments 服务。该服务由 @objectstack/service-storage (StorageServicePlugin) 注册。',
'ja-JP': '添付ファイルの削除・名前変更エンドポイントは利用できません。このカーネルには file-attachments サービスが登録されていません。このサービスは @objectstack/service-storage (StorageServicePlugin) が登録します。',
'es-ES': 'El punto de acceso para eliminar y renombrar adjuntos no está disponible: no hay ningún servicio file-attachments registrado en este kernel. Lo registra @objectstack/service-storage (StorageServicePlugin).',
'es-ES': 'Los puntos de acceso para eliminar y renombrar adjuntos no están disponibles: no hay ningún servicio file-attachments registrado en este kernel. Lo registra @objectstack/service-storage (StorageServicePlugin).',
},
memberMissing: {
en: 'The attachment removal and rename doors is not available: the registered file-attachments service does not implement handleAttachmentDoor.',
en: 'The attachment removal and rename doors are not available: the registered file-attachments service does not implement handleAttachmentDoor.',
'zh-CN': '附件删除和重命名接口不可用:已注册的 file-attachments 服务未实现 handleAttachmentDoor。',
'ja-JP': '添付ファイルの削除・名前変更エンドポイントは利用できません。登録されている file-attachments サービスは handleAttachmentDoor を実装していません。',
'es-ES': 'El punto de acceso para eliminar y renombrar adjuntos no está disponible: el servicio file-attachments registrado no implementa handleAttachmentDoor.',
'es-ES': 'Los puntos de acceso para eliminar y renombrar adjuntos no están disponibles: el servicio file-attachments registrado no implementa handleAttachmentDoor.',
},
},
];
Expand Down Expand Up @@ -521,8 +501,8 @@ describe('[#37352] the personal-view and attachment doors\' 501s follow the requ
}

it('the English the catalog keys carry is each bridge\'s noun, byte for byte', () => {
expect(BUILTIN_OPERATION_MESSAGES.en[PERSONAL_VIEWS_BRIDGE.nounKey!]).toBe(PERSONAL_VIEWS_BRIDGE.noun);
expect(BUILTIN_OPERATION_MESSAGES.en[ATTACHMENTS_BRIDGE.nounKey!]).toBe(ATTACHMENTS_BRIDGE.noun);
expect(BUILTIN_OPERATION_MESSAGES.en[PRINT_BRIDGE.nounKey!]).toBe(PRINT_BRIDGE.noun);
expect(BUILTIN_OPERATION_MESSAGES.en[PERSONAL_VIEWS_BRIDGE.nounKey]).toBe(PERSONAL_VIEWS_BRIDGE.noun);
expect(BUILTIN_OPERATION_MESSAGES.en[ATTACHMENTS_BRIDGE.nounKey]).toBe(ATTACHMENTS_BRIDGE.noun);
expect(BUILTIN_OPERATION_MESSAGES.en[PRINT_BRIDGE.nounKey]).toBe(PRINT_BRIDGE.noun);
});
});
38 changes: 22 additions & 16 deletions packages/runtime/src/domains/own-door-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@
* own for its 5xx). The bridge holds no `ExecutionContext` and no `i18n`
* service, so the header is the only source and no `errors.own_door_*`
* override is consulted. The door's noun is a catalog key too
* ({@link OwnDoorBridge.nounKey}); a bridge that names none keeps its English
* sentence whatever the language, so a sentence is never half translated.
* Only the language moves: the English is byte-identical, the code and the
* status are unchanged, and no `userMessage` is added. The slot, member and
* ({@link OwnDoorBridge.nounKey}), required so no bridge ships an English-only
* `501`; a plural noun ({@link OwnDoorBridge.nounPlural}) reads the `_plural`
* pair of sentences, so the English verb agrees. Only the language moves: the
* code and the status are unchanged, and no `userMessage` is added. The slot, member and
* package names are identifiers and stay as written.
*/

Expand Down Expand Up @@ -105,9 +105,14 @@ export interface OwnDoorBridge {
/**
* [#37273] The operation-message catalog key of {@link noun} (its English
* text is `noun`'s, byte for byte), which puts the bridge's `501` in the
* request's language. Absent, the `501` stays English.
* request's language. Required (#37610): a bridge cannot ship English-only.
*/
readonly nounKey?: string;
readonly nounKey: string;
/**
* [#37610] {@link noun} is plural (`the … doors`): the `501` reads the
* catalog's `_plural` sentences, so the English says "are".
*/
readonly nounPlural?: true;
/** The log tag, e.g. `personal-views`. */
readonly tag: string;
}
Expand Down Expand Up @@ -231,24 +236,25 @@ export async function handleOwnDoorBridgeRequest(
}
}

/**
* A `501` sentence of the catalog, in the request's top `Accept-Language` tag
* when the bridge names its noun's key, in English otherwise (the noun would
* stay English, and a sentence is never half translated).
*/
/** A `501` sentence of the catalog, in the request's top `Accept-Language` tag. */
function notImplementedSentence(
bridge: OwnDoorBridge,
context: HttpProtocolContext,
messageKey: 'own_door_service_unregistered' | 'own_door_member_missing',
params: Record<string, string>,
): string {
const caller = bridge.nounKey !== undefined ? { locale: requestLocaleOf(context) } : undefined;
const noun = bridge.nounKey !== undefined ? operationUserMessage(bridge.nounKey, caller) : bridge.noun;
return operationUserMessage(messageKey, caller, { door: capitalize(noun), slot: bridge.slot, ...params });
const caller = { locale: requestLocaleOf(context) };
const noun = operationUserMessage(bridge.nounKey, caller);
const key = bridge.nounPlural ? `${messageKey}_plural` : messageKey;
return operationUserMessage(key, caller, { door: capitalize(noun), slot: bridge.slot, ...params });
}

/** The request's top `Accept-Language` tag, from a fetch `Request` or a plain header record. */
function requestLocaleOf(context: HttpProtocolContext): string | undefined {
/**
* The request's top `Accept-Language` tag, from a fetch `Request` or a plain
* header record: the language of an own door's `501` (#37610: the comment and
* webhook redelivery doors read it too).
*/
export function requestLocaleOf(context: HttpProtocolContext): string | undefined {
const headers = (context.request as { headers?: unknown } | undefined)?.headers as
| { get?: (name: string) => string | null }
| Record<string, unknown>
Expand Down
Loading
Loading