-
Notifications
You must be signed in to change notification settings - Fork 0
feat(legal): allow partners to customise Terms of Use and Privacy Policy #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wilfredeveloper
wants to merge
2
commits into
main
Choose a base branch
from
feat/partner-customizable-legal-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // mute chatty console | ||
| import "src/_test_utilities/consoleMock"; | ||
|
|
||
| import { getLegalDocument } from "src/legal/legalDocumentLoader"; | ||
| import * as legalDocumentsConfig from "src/legal/legalDocumentsConfig"; | ||
| import { setupFetchSpy } from "src/_test_utilities/fetchSpy"; | ||
|
|
||
| describe("getLegalDocument", () => { | ||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| test("returns title from frontmatter and trimmed markdown body on a successful fetch", async () => { | ||
| // GIVEN a configured URL and a successful fetch returning markdown with a frontmatter title | ||
| jest | ||
| .spyOn(legalDocumentsConfig, "getLegalDocumentsConfig") | ||
| .mockReturnValue({ termsOfUse: "/legal/terms-of-use.md" }); | ||
| const markdown = '---\ntitle: "Custom Terms"\n---\n\n# Body'; | ||
| const fetchSpy = setupFetchSpy(200, markdown, ""); | ||
|
|
||
| // WHEN getLegalDocument is called for that variant | ||
| const result = await getLegalDocument("termsOfUse"); | ||
|
|
||
| // THEN the fetch is made against the configured URL and the parsed document is returned | ||
| expect(fetchSpy).toHaveBeenCalledWith("/legal/terms-of-use.md"); | ||
| expect(result.title).toBe("Custom Terms"); | ||
| expect(result.markdown).toBe("# Body"); | ||
| }); | ||
|
|
||
| test("falls back to a per-variant default title when the markdown has no frontmatter", async () => { | ||
| // GIVEN a configured URL and markdown without frontmatter | ||
| jest | ||
| .spyOn(legalDocumentsConfig, "getLegalDocumentsConfig") | ||
| .mockReturnValue({ privacyPolicy: "/legal/privacy-policy.md" }); | ||
| const markdown = "# Privacy body\n\nSome text."; | ||
| setupFetchSpy(200, markdown, ""); | ||
|
|
||
| // WHEN getLegalDocument is called | ||
| const result = await getLegalDocument("privacyPolicy"); | ||
|
|
||
| // THEN the default per-variant title is used | ||
| expect(result.title).toBe("Privacy Policy"); | ||
| expect(result.markdown).toContain("# Privacy body"); | ||
| }); | ||
|
|
||
| test("throws when no URL is configured for the requested variant", async () => { | ||
| // GIVEN no configured URL for the variant | ||
| jest.spyOn(legalDocumentsConfig, "getLegalDocumentsConfig").mockReturnValue({}); | ||
|
|
||
| // WHEN getLegalDocument is called THEN it rejects | ||
| await expect(getLegalDocument("termsOfUse")).rejects.toThrow( | ||
| /No URL configured for legal document variant: termsOfUse/ | ||
| ); | ||
| }); | ||
|
|
||
| test("throws when the fetch responds with a non-OK status", async () => { | ||
| // GIVEN a configured URL and a fetch returning 404 | ||
| jest | ||
| .spyOn(legalDocumentsConfig, "getLegalDocumentsConfig") | ||
| .mockReturnValue({ termsOfUse: "https://partner.example/terms.md" }); | ||
| setupFetchSpy(404, "not found", ""); | ||
|
|
||
| // WHEN getLegalDocument is called THEN it rejects with status detail | ||
| await expect(getLegalDocument("termsOfUse")).rejects.toThrow(/404/); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,36 @@ | ||
| import { parseYamlFrontmatter } from "src/utils/parseYamlFrontmatter"; | ||
| import { getLegalDocumentsConfig, LegalDocumentVariant } from "src/legal/legalDocumentsConfig"; | ||
|
|
||
| import privacyPolicyMd from "!!raw-loader!./documents/privacy-policy.md"; | ||
| import termsOfUseMd from "!!raw-loader!./documents/terms-of-use.md"; | ||
|
|
||
| export type LegalDocumentVariant = "privacy" | "terms"; | ||
|
|
||
| const documentRegistry: Record<LegalDocumentVariant, string> = { | ||
| privacy: privacyPolicyMd, | ||
| terms: termsOfUseMd, | ||
| }; | ||
| export type { LegalDocumentVariant }; | ||
|
|
||
| export interface LegalDocument { | ||
| title: string; | ||
| markdown: string; | ||
| } | ||
|
|
||
| export const getLegalDocument = (variant: LegalDocumentVariant): LegalDocument => { | ||
| const raw = documentRegistry[variant]; | ||
| const DEFAULT_TITLES: Record<LegalDocumentVariant, string> = { | ||
| termsOfUse: "Terms of Use", | ||
| privacyPolicy: "Privacy Policy", | ||
| }; | ||
|
|
||
| export const getLegalDocument = async (variant: LegalDocumentVariant): Promise<LegalDocument> => { | ||
| const url = getLegalDocumentsConfig()[variant]; | ||
| if (!url) { | ||
| throw new Error(`No URL configured for legal document variant: ${variant}`); | ||
| } | ||
|
|
||
| const response = await fetch(url); | ||
| if (!response.ok) { | ||
| throw new Error(`Failed to load ${variant} from ${url}: ${response.status}`); | ||
| } | ||
|
|
||
| const raw = await response.text(); | ||
| const { data, content } = parseYamlFrontmatter(raw); | ||
| // parseYamlFrontmatter returns "Untitled" when no frontmatter title is present; | ||
| // fall back to a sensible per-variant default in that case. | ||
| const hasExplicitTitle = typeof data.title === "string" && data.title.length > 0 && data.title !== "Untitled"; | ||
| return { | ||
| title: data.title, | ||
| title: hasExplicitTitle ? data.title : DEFAULT_TITLES[variant], | ||
| markdown: content.trimStart(), | ||
| }; | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If thre is no URL, I think it might be a good idea to return the default values instead of throwing an error.
for backward config compatibility.
What do you think?