Skip to content

Commit

Permalink
refactor: move 'getNextPageToken' to services layer
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard committed Feb 7, 2024
1 parent 99d1594 commit ca20f97
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
14 changes: 0 additions & 14 deletions core/api/src/domain/authentication/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion core/api/src/services/kratos/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
18 changes: 1 addition & 17 deletions core/api/test/unit/domain/authentication/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getNextPageToken, getSupportedCountries } from "@/domain/authentication"
import { getSupportedCountries } from "@/domain/authentication"

describe("getSupportedCountries", () => {
it("returns supported countries", () => {
Expand All @@ -20,19 +20,3 @@ describe("getSupportedCountries", () => {
])
})
})

describe("decoding link header", () => {
const withNext =
'</admin/clients?page_size=5&page_token=euKoY1BqY3J8GVax>; rel="first",</admin/clients?page_size=5&page_token=h9LfEKUiFoLH2R0A>; rel="next"'

const withoutNext =
'</admin/clients?page_size=5&page_token=euKoY1BqY3J8GVax>; 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)
})
})
17 changes: 17 additions & 0 deletions core/api/test/unit/services/kratos/identity.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { getNextPageToken } from "@/services/kratos"

describe("decoding link header", () => {
const withNext =
'</admin/clients?page_size=5&page_token=euKoY1BqY3J8GVax>; rel="first",</admin/clients?page_size=5&page_token=h9LfEKUiFoLH2R0A>; rel="next"'

const withoutNext =
'</admin/clients?page_size=5&page_token=euKoY1BqY3J8GVax>; rel="first"'

it("try decoding link successfully", () => {
expect(getNextPageToken(withNext)).toBe("h9LfEKUiFoLH2R0A")

Check warning on line 11 in core/api/test/unit/services/kratos/identity.spec.ts

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"Fo" should be "Of" or "For" or "Do" or "Go" or "To".
})

it("should be undefined when no more next is present", () => {
expect(getNextPageToken(withoutNext)).toBe(undefined)
})
})

0 comments on commit ca20f97

Please sign in to comment.