Skip to content
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

fix(docs): use the headers arg for auth examples vs. more verbose fetchWrapper #1907

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions examples/auth/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@ interface UserAccumulator {
[key: number]: User[]
}

const fetchWrapper = async (...args: Parameters<typeof fetch>) => {
const queryParams = new URLSearchParams(window.location.search)
const org_id = queryParams.get(`org_id`)
const modifiedArgs = [...args]
if (org_id) {
const headers = new Headers((modifiedArgs[1] as RequestInit)?.headers || {})
headers.set(`Authorization`, org_id)
modifiedArgs[1] = { ...(modifiedArgs[1] as RequestInit), headers }
}
const response = await fetch(...(modifiedArgs as [RequestInfo, RequestInit?]))
return response
}

const usersShape = (): ShapeStreamOptions => {
if (typeof window !== `undefined`) {
const queryParams = new URLSearchParams(window.location.search)
Expand All @@ -37,7 +24,9 @@ const usersShape = (): ShapeStreamOptions => {
`/shape-proxy/users?org_id=${org_id}`,
window.location.origin
).href,
fetchClient: fetchWrapper,
headers: {
Authorization: org_id || ``,
},
}
} else {
return {
Expand Down
21 changes: 4 additions & 17 deletions website/docs/guides/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,14 @@ When using the [Typescript client](/docs/api/clients/typescript), you can pass a
In the client:

```tsx
const fetchWrapper = async (...args: Parameters<typeof fetch>) => {
const usersShape = (): ShapeStreamOptions => {
const user = loadCurrentUser()
const modifiedArgs = [...args]
const headers = new Headers(
(modifiedArgs[1] as RequestInit)?.headers || {}
)

// Set authorization token
headers.set(`Authorization`, `Bearer ${user.token}`)

modifiedArgs[1] = { ...(modifiedArgs[1] as RequestInit), headers }
const response = await fetch(
...(modifiedArgs as [RequestInfo, RequestInit?])
)
return response
}

const usersShape = (): ShapeStreamOptions => {
return {
url: new URL(`/api/shapes/users`, window.location.origin).href,
fetchClient: fetchWrapper,
headers: {
authorization: `Bearer ${user.token}`
}
}
}

Expand Down
Loading