From d4f38bce2b016ddd4e6d5f260e04c7e0f4f312f7 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Wed, 1 Jun 2022 16:56:23 +0800 Subject: [PATCH] feat(core): add etag for settings api (#1011) --- packages/core/package.json | 2 + packages/core/src/routes/sign-in-settings.ts | 53 +++++++++++++------- pnpm-lock.yaml | 11 +++- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 8bb5f6703..1663cb94d 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -44,6 +44,7 @@ "decamelize": "^5.0.0", "deepmerge": "^4.2.2", "dotenv": "^16.0.0", + "etag": "^1.8.1", "got": "^11.8.2", "i18next": "^21.0.0", "iconv-lite": "0.6.3", @@ -76,6 +77,7 @@ "@silverhand/jest-config": "^0.14.0", "@silverhand/ts-config": "^0.14.0", "@types/debug": "^4.1.7", + "@types/etag": "^1.8.1", "@types/inquirer": "^8.2.1", "@types/jest": "^27.4.1", "@types/koa": "^2.13.3", diff --git a/packages/core/src/routes/sign-in-settings.ts b/packages/core/src/routes/sign-in-settings.ts index 3c2af8998..ed803ccde 100644 --- a/packages/core/src/routes/sign-in-settings.ts +++ b/packages/core/src/routes/sign-in-settings.ts @@ -1,4 +1,5 @@ import { ConnectorMetadata } from '@logto/connector-types'; +import etag from 'etag'; import { getConnectorInstances } from '@/connectors'; import { findDefaultSignInExperience } from '@/queries/sign-in-experience'; @@ -6,25 +7,39 @@ import { findDefaultSignInExperience } from '@/queries/sign-in-experience'; import { AnonymousRouter } from './types'; export default function signInSettingsRoutes(router: T) { - router.get('/sign-in-settings', async (ctx, next) => { - const [signInExperience, connectorInstances] = await Promise.all([ - findDefaultSignInExperience(), - getConnectorInstances(), - ]); - const socialConnectors = signInExperience.socialSignInConnectorTargets.reduce< - Array - >((previous, connectorTarget) => { - const connectors = connectorInstances.filter( - ({ metadata: { target }, connector: { enabled } }) => target === connectorTarget && enabled - ); + router.get( + '/sign-in-settings', + async (ctx, next) => { + const [signInExperience, connectorInstances] = await Promise.all([ + findDefaultSignInExperience(), + getConnectorInstances(), + ]); + const socialConnectors = signInExperience.socialSignInConnectorTargets.reduce< + Array + >((previous, connectorTarget) => { + const connectors = connectorInstances.filter( + ({ metadata: { target }, connector: { enabled } }) => + target === connectorTarget && enabled + ); - return [ - ...previous, - ...connectors.map(({ metadata, connector: { id } }) => ({ ...metadata, id })), - ]; - }, []); - ctx.body = { ...signInExperience, socialConnectors }; + return [ + ...previous, + ...connectors.map(({ metadata, connector: { id } }) => ({ ...metadata, id })), + ]; + }, []); + ctx.body = { ...signInExperience, socialConnectors }; - return next(); - }); + return next(); + }, + async (ctx, next) => { + await next(); + + ctx.response.etag = etag(JSON.stringify(ctx.body)); + + if (ctx.fresh) { + ctx.status = 304; + ctx.body = null; + } + } + ); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2562864d0..339fad501 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -767,6 +767,7 @@ importers: '@silverhand/jest-config': ^0.14.0 '@silverhand/ts-config': ^0.14.0 '@types/debug': ^4.1.7 + '@types/etag': ^1.8.1 '@types/inquirer': ^8.2.1 '@types/jest': ^27.4.1 '@types/koa': ^2.13.3 @@ -786,6 +787,7 @@ importers: deepmerge: ^4.2.2 dotenv: ^16.0.0 eslint: ^8.10.0 + etag: ^1.8.1 got: ^11.8.2 i18next: ^21.0.0 iconv-lite: 0.6.3 @@ -847,6 +849,7 @@ importers: decamelize: 5.0.1 deepmerge: 4.2.2 dotenv: 16.0.0 + etag: 1.8.1 got: 11.8.3 i18next: 21.6.12 iconv-lite: 0.6.3 @@ -878,6 +881,7 @@ importers: '@silverhand/jest-config': 0.14.0_makj2rl6gt73u7koqro542qsmm '@silverhand/ts-config': 0.14.0_typescript@4.6.2 '@types/debug': 4.1.7 + '@types/etag': 1.8.1 '@types/inquirer': 8.2.1 '@types/jest': 27.4.1 '@types/koa': 2.13.4 @@ -7390,6 +7394,12 @@ packages: resolution: {integrity: sha512-CuPgU6f3eT/XgKKPqKd/gLZV1Xmvf1a2R5POBOGQa6uv82xpls89HU5zKeVoyR8XzHd1RGNOlQlvUe3CFkjWNQ==} dev: true + /@types/etag/1.8.1: + resolution: {integrity: sha512-bsKkeSqN7HYyYntFRAmzcwx/dKW4Wa+KVMTInANlI72PWLQmOpZu96j0OqHZGArW4VQwCmJPteQlXaUDeOB0WQ==} + dependencies: + '@types/node': 17.0.23 + dev: true + /@types/express-serve-static-core/4.17.26: resolution: {integrity: sha512-zeu3tpouA043RHxW0gzRxwCHchMgftE8GArRsvYT0ByDMbn19olQHx5jLue0LxWY6iYtXb7rXmuVtSkhy9YZvQ==} dependencies: @@ -11511,7 +11521,6 @@ packages: /etag/1.8.1: resolution: {integrity: sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==} engines: {node: '>= 0.6'} - dev: true /eval/0.1.8: resolution: {integrity: sha512-EzV94NYKoO09GLXGjXj9JIlXijVck4ONSr5wiCWDvhsvj5jxSrzTmRU/9C1DyB6uToszLs8aifA6NQ7lEQdvFw==}