-
Notifications
You must be signed in to change notification settings - Fork 1
[DEV-1951] Add urlreplacemap codec, + tests + mapping function #1166
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
Merged
Merged
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
1634b99
add url replace map single type
MarBert c3f272b
add changeset
MarBert c0093d2
refactor for linting
MarBert e9dfb8d
add codec, fetch and make for urlreplacemap
MarBert 3bfc82e
add required to url
MarBert 8606f2a
Merge branch 'DEV-1950-add-urlreplacemap-to-strapi' into DEV-1951-add…
MarBert c700529
test, populate, refactor
petros-double-test1 9147ad0
Merge branch 'main' into DEV-1951-add-urlreplacemap-codec
tommaso1 7944f3d
Merge branch 'main' into DEV-1951-add-urlreplacemap-codec
MarBert 5b803bf
Update apps/nextjs-website/src/lib/strapi/__tests__/urlReplaceMap.tes…
tommaso1 b77b667
Update apps/nextjs-website/src/lib/strapi/__tests__/urlReplaceMap.tes…
tommaso1 85c919d
fix import
petros-double-test1 349b0a9
Merge branch 'main' into DEV-1951-add-urlreplacemap-codec
tommaso1 20dab09
Merge branch 'main' into DEV-1951-add-urlreplacemap-codec
tommaso1 d9a4ea7
Update apps/nextjs-website/src/lib/strapi/codecs/UrlReplaceMapCodec.ts
tommaso1 938019c
Fix test
petros-double-test1 831ca5c
delete healthy cobras
petros-double-test1 75aa67c
add icon
petros-double-test1 80ffa18
rename type
petros-double-test1 9736ab4
rename type
petros-double-test1 51f5ccc
changeset
petros-double-test1 59aaf53
Merge branch 'main' into DEV-1951-add-urlreplacemap-codec
marcobottaro 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "strapi-cms": minor | ||
| --- | ||
|
|
||
| Add UrlReplaceMap as a single type |
78 changes: 78 additions & 0 deletions
78
apps/nextjs-website/src/lib/strapi/__tests__/urlReplaceMap.test.ts
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,78 @@ | ||
| import { UrlReplaceMapCodec } from '../codecs/UrlReplaceMapCodec'; | ||
| import * as Either from 'fp-ts/lib/Either'; | ||
| import { baseProductJson } from './fixtures/product'; | ||
| import { makeUrlReplaceMap } from '../makeProps/makeUrlReplaceMap'; | ||
| import { pipe } from 'fp-ts/lib/function'; | ||
| import { mediaRasterJson } from './fixtures/media'; | ||
|
|
||
| const strapiResponse = { | ||
| data: { | ||
| id: 1, | ||
| attributes: { | ||
| createdAt: '2024-09-30T10:12:44.888Z', | ||
| updatedAt: '2024-09-30T10:12:45.562Z', | ||
| publishedAt: '2024-09-30T10:12:45.561Z', | ||
| urlToGuide: [ | ||
| { | ||
| id: 1, | ||
| url: 'aaaa', | ||
| version: '2', | ||
| guide: { | ||
| data: { | ||
| id: 1, | ||
| attributes: { | ||
| title: 'aaa', | ||
| slug: 'aaaa', | ||
| createdAt: '2024-09-30T10:12:08.182Z', | ||
| updatedAt: '2024-09-30T10:12:24.805Z', | ||
| publishedAt: '2024-09-30T10:12:24.802Z', | ||
| locale: 'en', | ||
| image: mediaRasterJson, | ||
| mobileImage: mediaRasterJson, | ||
| listItems: [ | ||
| { id: 1, text: 'rhhtrhrthrt' }, | ||
| { id: 2, text: 'fdsafdsagtyhtyh' }, | ||
| { id: 3, text: 'hrhrhthrtyrth' }, | ||
| ], | ||
| product: baseProductJson, | ||
| versions: [], | ||
| seo: null, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| meta: {}, | ||
| }; | ||
|
|
||
| describe('UrlReplaceMapCodec', () => { | ||
| it('should decode strapi UrlReplaceMapCodec', () => { | ||
| const decodeResponse = UrlReplaceMapCodec.decode(strapiResponse); | ||
| expect(Either.isRight(decodeResponse)).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('Should correctly convert the map into a record', () => { | ||
| const validation = UrlReplaceMapCodec.decode(strapiResponse); | ||
| const result = pipe( | ||
| validation, | ||
| Either.fold( | ||
| (left) => { | ||
| console.error('Error:', left); | ||
| return null; // or handle the error as needed | ||
| }, | ||
| (right) => { | ||
| console.log('Success:', right); | ||
| return right; | ||
| } | ||
| ) | ||
| ); | ||
| // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
| const urlReplaceMap = makeUrlReplaceMap(result!); | ||
|
|
||
| expect(urlReplaceMap).toEqual({ | ||
| aaaa: 'firma-con-io/guides/aaaa/2', | ||
| }); | ||
| }); | ||
| }); |
22 changes: 22 additions & 0 deletions
22
apps/nextjs-website/src/lib/strapi/codecs/UrlReplaceMapCodec.ts
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,22 @@ | ||
| import * as t from 'io-ts/lib'; | ||
| import { GuideCodec } from './GuidesCodec'; | ||
| import { NullToUndefinedCodec } from './NullToUndefinedCodec'; | ||
|
|
||
| const UrlToGuideCodec = t.strict({ | ||
| id: t.number, | ||
| url: t.string, | ||
| version: t.union([NullToUndefinedCodec, t.string]), | ||
| guide: t.strict({ | ||
| data: GuideCodec, | ||
tommaso1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }), | ||
| }); | ||
|
|
||
| export const UrlReplaceMapCodec = t.strict({ | ||
| data: t.strict({ | ||
| attributes: t.strict({ | ||
| urlToGuide: t.array(UrlToGuideCodec), | ||
| }), | ||
| }), | ||
| }); | ||
|
|
||
| export type UrlReplaceMap = t.TypeOf<typeof UrlReplaceMapCodec>; | ||
15 changes: 15 additions & 0 deletions
15
apps/nextjs-website/src/lib/strapi/fetches/fetchUrlReplaceMap.ts
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,15 @@ | ||
| import { fetchFromStrapi } from '../fetchFromStrapi'; | ||
| import { UrlReplaceMapCodec } from '../codecs/UrlReplaceMapCodec'; | ||
| import qs from 'qs'; | ||
|
|
||
| const makeStrapiUrlReplaceMapPopulate = () => | ||
| qs.stringify({ | ||
| populate: | ||
| 'urlToGuide.guide.*,urlToGuide.guide.image,urlToGuide.guide.mobileImage,urlToGuide.guide.listItems,urlToGuide.guide.product.*,urlToGuide.guide.versions.*,urlToGuide.guide.seo.*', | ||
| }); | ||
|
|
||
| export const fetchUrlReplaceMap = fetchFromStrapi( | ||
| 'url-replace-map', | ||
| makeStrapiUrlReplaceMapPopulate(), | ||
| UrlReplaceMapCodec | ||
| ); |
19 changes: 19 additions & 0 deletions
19
apps/nextjs-website/src/lib/strapi/makeProps/makeUrlReplaceMap.ts
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,19 @@ | ||
| import { UrlReplaceMap } from '../codecs/UrlReplaceMapCodec'; | ||
|
|
||
| export type UrlReplacesMap = Record<string, string>; | ||
|
|
||
| export function makeUrlReplaceMap( | ||
| urlReplacemap: UrlReplaceMap | ||
| ): UrlReplacesMap { | ||
| const map = urlReplacemap.data.attributes.urlToGuide.reduce((map, obj) => { | ||
| return { | ||
| ...map, | ||
| [obj.url]: `${ | ||
| obj.guide.data?.attributes.product.data.attributes.slug | ||
| }/guides/${obj.guide.data?.attributes.slug}${ | ||
| obj.version ? `/${obj.version}` : '' | ||
| }`, | ||
| }; | ||
| }, {} as UrlReplacesMap); | ||
| return map; | ||
| } |
22 changes: 22 additions & 0 deletions
22
apps/strapi-cms/src/api/url-replace-map/content-types/url-replace-map/schema.json
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,22 @@ | ||
| { | ||
| "kind": "singleType", | ||
| "collectionName": "url_replace_maps", | ||
| "info": { | ||
| "singularName": "url-replace-map", | ||
| "pluralName": "url-replace-maps", | ||
| "displayName": "UrlReplaceMap", | ||
| "description": "" | ||
| }, | ||
| "options": { | ||
| "draftAndPublish": true | ||
| }, | ||
| "pluginOptions": {}, | ||
| "attributes": { | ||
| "urlToGuide": { | ||
| "displayName": "urlToGuide", | ||
| "type": "component", | ||
| "repeatable": true, | ||
| "component": "products.url-to-guide" | ||
| } | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
apps/strapi-cms/src/api/url-replace-map/controllers/url-replace-map.ts
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,9 @@ | ||
| /** | ||
| * url-replace-map controller | ||
| */ | ||
|
|
||
| import { factories } from '@strapi/strapi'; | ||
|
|
||
| export default factories.createCoreController( | ||
| 'api::url-replace-map.url-replace-map' | ||
| ); |
9 changes: 9 additions & 0 deletions
9
apps/strapi-cms/src/api/url-replace-map/routes/url-replace-map.ts
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,9 @@ | ||
| /** | ||
| * url-replace-map router | ||
| */ | ||
|
|
||
| import { factories } from '@strapi/strapi'; | ||
|
|
||
| export default factories.createCoreRouter( | ||
| 'api::url-replace-map.url-replace-map' | ||
| ); |
9 changes: 9 additions & 0 deletions
9
apps/strapi-cms/src/api/url-replace-map/services/url-replace-map.ts
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,9 @@ | ||
| /** | ||
| * url-replace-map service | ||
| */ | ||
|
|
||
| import { factories } from '@strapi/strapi'; | ||
|
|
||
| export default factories.createCoreService( | ||
| 'api::url-replace-map.url-replace-map' | ||
| ); |
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,22 @@ | ||
| { | ||
| "collectionName": "components_products_url_to_guides", | ||
| "info": { | ||
| "displayName": "urlToGuide", | ||
| "description": "" | ||
| }, | ||
| "options": {}, | ||
| "attributes": { | ||
| "url": { | ||
| "type": "string", | ||
| "required": true | ||
| }, | ||
| "guide": { | ||
| "type": "relation", | ||
| "relation": "oneToOne", | ||
| "target": "api::guide.guide" | ||
| }, | ||
| "version": { | ||
| "type": "string" | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.