From ca20f97e8d59905ea4e665ec9111fbedfc3a6fed Mon Sep 17 00:00:00 2001 From: vindard <17693119+vindard@users.noreply.github.com> Date: Wed, 7 Feb 2024 17:09:35 -0400 Subject: [PATCH] refactor: move 'getNextPageToken' to services layer --- core/api/src/domain/authentication/index.ts | 14 -------------- core/api/src/services/kratos/identity.ts | 15 ++++++++++++++- .../unit/domain/authentication/index.spec.ts | 18 +----------------- .../test/unit/services/kratos/identity.spec.ts | 17 +++++++++++++++++ 4 files changed, 32 insertions(+), 32 deletions(-) create mode 100644 core/api/test/unit/services/kratos/identity.spec.ts diff --git a/core/api/src/domain/authentication/index.ts b/core/api/src/domain/authentication/index.ts index 13456cc136..aa0b5917bf 100644 --- a/core/api/src/domain/authentication/index.ts +++ b/core/api/src/domain/authentication/index.ts @@ -35,20 +35,6 @@ export const getSupportedCountries = ({ return countries } -export const getNextPageToken = (link: string): string | undefined => { - const links = link.split(",") - const nextLink = links.find((link) => link.includes('rel="next"')) - - if (nextLink) { - const matches = nextLink.match(/page_token=([^;&>]+)/) - if (matches) { - return matches[1] - } - } - - return undefined -} - export const checkedToEmailCode = (code: string): EmailCode | ApplicationError => { if (!/^[0-9]{6}$/.test(code)) return new EmailCodeInvalidError() return code as EmailCode diff --git a/core/api/src/services/kratos/identity.ts b/core/api/src/services/kratos/identity.ts index 34f6d98ec7..ec81526483 100644 --- a/core/api/src/services/kratos/identity.ts +++ b/core/api/src/services/kratos/identity.ts @@ -7,7 +7,20 @@ import { getKratosPostgres, kratosAdmin, toDomainIdentity } from "./private" import { IdentifierNotFoundError } from "@/domain/authentication/errors" import { ErrorLevel } from "@/domain/shared" -import { getNextPageToken } from "@/domain/authentication" + +export const getNextPageToken = (link: string): string | undefined => { + const links = link.split(",") + const nextLink = links.find((link) => link.includes('rel="next"')) + + if (nextLink) { + const matches = nextLink.match(/page_token=([^;&>]+)/) + if (matches) { + return matches[1] + } + } + + return undefined +} export const IdentityRepository = (): IIdentityRepository => { const getIdentity = async ( diff --git a/core/api/test/unit/domain/authentication/index.spec.ts b/core/api/test/unit/domain/authentication/index.spec.ts index ebfbc1a05b..83b9d8b252 100644 --- a/core/api/test/unit/domain/authentication/index.spec.ts +++ b/core/api/test/unit/domain/authentication/index.spec.ts @@ -1,4 +1,4 @@ -import { getNextPageToken, getSupportedCountries } from "@/domain/authentication" +import { getSupportedCountries } from "@/domain/authentication" describe("getSupportedCountries", () => { it("returns supported countries", () => { @@ -20,19 +20,3 @@ describe("getSupportedCountries", () => { ]) }) }) - -describe("decoding link header", () => { - const withNext = - '; rel="first",; rel="next"' - - const withoutNext = - '; rel="first"' - - it("try decoding link successfully", () => { - expect(getNextPageToken(withNext)).toBe("h9LfEKUiFoLH2R0A") - }) - - it("should be undefined when no more next is present", () => { - expect(getNextPageToken(withoutNext)).toBe(undefined) - }) -}) diff --git a/core/api/test/unit/services/kratos/identity.spec.ts b/core/api/test/unit/services/kratos/identity.spec.ts new file mode 100644 index 0000000000..349f46714a --- /dev/null +++ b/core/api/test/unit/services/kratos/identity.spec.ts @@ -0,0 +1,17 @@ +import { getNextPageToken } from "@/services/kratos" + +describe("decoding link header", () => { + const withNext = + '; rel="first",; rel="next"' + + const withoutNext = + '; rel="first"' + + it("try decoding link successfully", () => { + expect(getNextPageToken(withNext)).toBe("h9LfEKUiFoLH2R0A") + }) + + it("should be undefined when no more next is present", () => { + expect(getNextPageToken(withoutNext)).toBe(undefined) + }) +})