generated from unjs/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
automatically configure sanity cors origin and preview token
- Loading branch information
Showing
10 changed files
with
126 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules | ||
studio | ||
coverage | ||
dist | ||
.vscode | ||
|
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,5 +1,5 @@ | ||
{ | ||
"printWidth": 120, | ||
"printWidth": 80, | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"semi": false, | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function extractSanityCliToken(output: string): string | null { | ||
// This regular expression looks for the phrase "Auth token: " followed by any characters | ||
// that are not whitespace, capturing this sequence into a group. | ||
const regex = /Auth token:\s+(\S+)/ | ||
const match = output.match(regex) | ||
|
||
// If a match is found, return the first captured group, which contains the token, split on single quotes and get the token itself. | ||
// Otherwise, return null if no match is found. | ||
return match?.[1]?.split(`'`)[1] ?? null | ||
} |
2 changes: 1 addition & 1 deletion
2
src/utils/extract-project-id.ts → src/utils/extract-sanity-project-id.ts
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,2 +1,4 @@ | ||
export { extractProjectId } from './extract-project-id' | ||
export { createSanityCli } from './sanity-cli' | ||
export { extractSanityProjectId } from './extract-sanity-project-id' | ||
export { extractSanityCliToken } from './extract-sanity-cli-token' | ||
export { createEnvContent } from './create-env-content' |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export function createSanityCli({ | ||
projectId, | ||
token, | ||
}: { | ||
projectId: string | ||
token: string | ||
}) { | ||
return async (endpoint: string, body: Record<string, any>) => { | ||
const response = await fetch( | ||
`https://api.sanity.io/v2021-06-07/projects/${projectId}${endpoint}`, | ||
{ | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
body: JSON.stringify(body), | ||
}, | ||
) | ||
|
||
if (!response.ok) { | ||
throw new Error(`The Sanity CLI response was not ok.`) | ||
} | ||
|
||
return await response.json() | ||
} | ||
} |