-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor create-adaptor, add to demo and fix issues with Exter…
…nalInput
- Loading branch information
Showing
8 changed files
with
100 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,15 +1,40 @@ | ||
export const createAdaptor = ( | ||
import { Adaptor } from "@measured/puck"; | ||
|
||
function createAdaptor( | ||
name: string, | ||
input: RequestInfo | URL, | ||
map?: (data: any) => Record<string, any>[] | ||
): Adaptor; | ||
|
||
function createAdaptor( | ||
name: string, | ||
input: RequestInfo | URL, | ||
init?: RequestInit, | ||
name: string = "API" | ||
) => ({ | ||
name, | ||
fetchList: async () => { | ||
const res = await fetch(input, init); | ||
const body: { data: Record<string, any>[] } = await res.json(); | ||
|
||
return body.data; | ||
}, | ||
}); | ||
map?: (data: any) => Record<string, any>[] | ||
): Adaptor; | ||
|
||
function createAdaptor( | ||
name: string, | ||
input: RequestInfo | URL, | ||
initOrMap?: unknown, | ||
map?: (data: any) => Record<string, any>[] | ||
): Adaptor { | ||
return { | ||
name, | ||
fetchList: async () => { | ||
if (typeof initOrMap === "function") { | ||
const res = await fetch(input); | ||
const body = await res.json(); | ||
|
||
return initOrMap(body); | ||
} | ||
|
||
const res = await fetch(input, initOrMap as RequestInit); | ||
const body = await res.json(); | ||
|
||
return map ? map(body) : body; | ||
}, | ||
}; | ||
} | ||
|
||
export default createAdaptor; |
This file contains 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,27 +1,19 @@ | ||
import { Adaptor } from "@measured/puck/types/Config"; | ||
import createAdaptor from "@measured/puck-adaptor-fetch/index"; | ||
|
||
type AdaptorParams = { apiToken: string; url: string; resource: string }; | ||
const createStrapiAdaptor = (url: string, apiToken: string) => | ||
createAdaptor( | ||
"Strapi.js", | ||
url, | ||
{ | ||
headers: { | ||
Authorization: `bearer ${apiToken}`, | ||
}, | ||
}, | ||
(body) => | ||
body.data.map(({ attributes, ...item }) => ({ | ||
...item, | ||
...attributes, | ||
})) | ||
); | ||
|
||
const strapiAdaptor: Adaptor<AdaptorParams> = { | ||
name: "Strapi.js", | ||
fetchList: async (adaptorParams) => { | ||
if (!adaptorParams) { | ||
return null; | ||
} | ||
|
||
const res = await fetch( | ||
`${adaptorParams.url}/api/${adaptorParams.resource}`, | ||
{ | ||
headers: { | ||
Authorization: `bearer ${adaptorParams.apiToken}`, | ||
}, | ||
} | ||
); | ||
|
||
const body: { data: Record<string, any>[] } = await res.json(); | ||
|
||
return body.data; | ||
}, | ||
}; | ||
|
||
export default strapiAdaptor; | ||
export default createStrapiAdaptor; |
This file contains 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 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 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