diff --git a/packages/account-client/src/client.ts b/packages/account-client/src/client.ts index 91d3a6d427..6cca8bc2c5 100644 --- a/packages/account-client/src/client.ts +++ b/packages/account-client/src/client.ts @@ -221,7 +221,7 @@ class AccountClientImpl implements AccountClient { private async _rpc(request: Request): Promise { const timezone = getClientTimezone() - const meta: Record = timezone !== undefined ? { 'X-Timezone': timezone } : {} + const meta: Record = timezone !== undefined ? { 'x-timezone': timezone } : {} const response = await fetch(this.url, { ...this.request, headers: { diff --git a/server/account-service/src/index.ts b/server/account-service/src/index.ts index 2df5dea7d5..7bfa37d865 100644 --- a/server/account-service/src/index.ts +++ b/server/account-service/src/index.ts @@ -4,6 +4,7 @@ import account, { type AccountMethods, + type Meta, EndpointKind, accountId, getAccountDB, @@ -173,6 +174,10 @@ export function serveAccount (measureCtx: MeasureContext, brandings: BrandingMap return extractAuthorizationToken(headers) ?? extractCookieToken(headers) } + const getRequestMeta = (headers: IncomingHttpHeaders): Meta => { + return headers?.['x-timezone'] !== undefined ? { timezone: headers['x-timezone'] as string } : {} + } + function getCookieOptions (ctx: Koa.Context): Cookies.SetOption { const requestUrl = ctx.request.href const url = new URL(requestUrl) @@ -314,6 +319,7 @@ export function serveAccount (measureCtx: MeasureContext, brandings: BrandingMap router.post('rpc', '/', async (ctx) => { const token = extractToken(ctx.request.headers) + const meta = getRequestMeta(ctx.request.headers) const request = ctx.request.body as any const method = methods[request.method as AccountMethods] @@ -339,7 +345,7 @@ export function serveAccount (measureCtx: MeasureContext, brandings: BrandingMap } const branding = host !== undefined ? brandings[host] : null const result = await measureCtx.with(request.method, {}, (mctx) => { - if (method === undefined) { + if (method === undefined || typeof method !== 'function') { const response = { id: request.id, error: new Status(Severity.ERROR, platform.status.UnknownMethod, { method: request.method }) @@ -349,7 +355,7 @@ export function serveAccount (measureCtx: MeasureContext, brandings: BrandingMap return } - return method(mctx, db, branding, request, token) + return method(mctx, db, branding, request, token, meta) }) const body = JSON.stringify(result) diff --git a/server/account/src/__tests__/utils.test.ts b/server/account/src/__tests__/utils.test.ts index 327f315b99..8d111c39ce 100644 --- a/server/account/src/__tests__/utils.test.ts +++ b/server/account/src/__tests__/utils.test.ts @@ -554,7 +554,7 @@ describe('account utils', () => { param1: 'value1', param2: 'value2' }, - {} + undefined ) }) @@ -567,7 +567,7 @@ describe('account utils', () => { const result = await wrappedMethod(mockCtx, mockDb, mockBranding, request, 'token') expect(result).toEqual({ id: 'req1', result: mockResult }) - expect(mockMethod).toHaveBeenCalledWith(mockCtx, mockDb, mockBranding, 'token', { param1: 'value1' }, {}) + expect(mockMethod).toHaveBeenCalledWith(mockCtx, mockDb, mockBranding, 'token', { param1: 'value1' }, undefined) }) test('should handle PlatformError', async () => { @@ -623,9 +623,9 @@ describe('account utils', () => { const mockMethod = jest.fn().mockResolvedValue(mockResult) const wrappedMethod = wrap(mockMethod) const mockTimezone = 'America/New_York' - const request = { id: 'req1', params: { param1: 'value1' }, headers: { 'X-Timezone': mockTimezone } } + const request = { id: 'req1', params: { param1: 'value1' } } - const result = await wrappedMethod(mockCtx, mockDb, mockBranding, request, 'token') + const result = await wrappedMethod(mockCtx, mockDb, mockBranding, request, 'token', { timezone: mockTimezone }) expect(result).toEqual({ id: 'req1', result: mockResult }) expect(mockMethod).toHaveBeenCalledWith( diff --git a/server/account/src/utils.ts b/server/account/src/utils.ts index 6c629c921e..93f26bb9b0 100644 --- a/server/account/src/utils.ts +++ b/server/account/src/utils.ts @@ -119,12 +119,9 @@ export function wrap ( db: AccountDB, branding: Branding | null, request: any, - token?: string + token?: string, + meta?: Meta ): Promise { - const meta = - request.headers !== undefined && request.headers['X-Timezone'] !== undefined - ? { timezone: request.headers['X-Timezone'] } - : {} return await accountMethod(ctx, db, branding, token, { ...request.params }, meta) .then((result) => ({ id: request.id, result })) .catch((err) => {