feat(#964): Add Hooks provider#1062
Conversation
commit: |
commit: |
docs: improve documentation
| ## `createRequest(data, authState, nuxt)` | ||
|
|
||
| Prepare data for the fetch call. | ||
|
|
||
| Must return either an object conforming to: | ||
|
|
||
| ```ts | ||
| interface CreateRequestResult { | ||
| // Path to the endpoint | ||
| path: string | ||
| // Request: body, headers, etc. | ||
| request: NitroFetchOptions | ||
| } | ||
| ``` |
There was a problem hiding this comment.
Why did you decide to abstract the returns of the hooks provider? While I think it is a good step forward compared to the current local-provider I think it could still limit you if you e.g. want to switch from ofetch to axios under the hood.
| ### `authState` argument | ||
|
|
||
| This argument gives you access to the state of the module, allowing to read or modify session data or tokens. |
There was a problem hiding this comment.
Do we have a schema for this or this is e.g. based off the session object you can provide via the nuxt config?
|
|
||
| This argument gives you access to the state of the module, allowing to read or modify session data or tokens. | ||
|
|
||
| ### `nuxt` argument |
There was a problem hiding this comment.
Should we call this nuxtApp like Nuxt also does in the docs you linked below?
| Handle the response and optionally instruct the module how to update state. | ||
|
|
||
| May return: | ||
| * `false` — stop further processing (module will not update auth state). |
There was a problem hiding this comment.
Does this false act the same way as the return false from the createRequest hook?
| * `ResponseAccept` object — instruct the module what to set in `authState` (see below). | ||
| * Throw an `Error` to propagate a failure. | ||
|
|
||
| The `response` argument is the [`ofetch` raw response](https://github.com/unjs/ofetch?tab=readme-ov-file#-access-to-raw-response) that the module uses as well. `response._data` usually contains parsed body. |
There was a problem hiding this comment.
What else can response._data contain if not the parsed body? E.g. do we do error handling here or just return whatever was in the body?
| onResponse(response) { | ||
| // Backend returns { access: 'xxx', refresh: 'yyy', user: {...} } | ||
| const body = response._data | ||
| // Default to `undefined` to not reset the tokens and session (but you may want to reset it) | ||
| return { | ||
| token: body?.access ?? undefined, | ||
| refreshToken: body?.refresh ?? undefined, | ||
| session: body?.user ?? undefined, | ||
| } | ||
| }, |
There was a problem hiding this comment.
Are these hooks promises? E.g. if I wanted to do another DB or API call here, would the hooks provider support this?
| } | ||
| }, | ||
|
|
||
| onResponse(response) { |
There was a problem hiding this comment.
If I return false from the getSession.createRequest hook is this response:
falsenull/undefinedonResponseis never called
|
|
||
| This argument gives you access to the state of the module, allowing to read or modify session data or tokens. | ||
|
|
||
| ### `nuxt` argument |
There was a problem hiding this comment.
Do we want to add a guide on how you can use this to e.g. access runtimeConfig variables? I think this could be a pretty common workflow.
🔗 Linked issue
Closes #964
❓ Type of change
📚 Description
📝 Checklist