Skip to content

Commit

Permalink
fix(composables): handle single types (#310)
Browse files Browse the repository at this point in the history
benjamincanac authored Jan 19, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 41e0be0 commit ca5b358
Showing 5 changed files with 37 additions and 11 deletions.
19 changes: 14 additions & 5 deletions docs/content/3.usage.md
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ Learn how to use the strapi module in your Nuxt 3 application.

> This module exposes composables that are [auto-imported](https://nuxt.com/docs/guide/directory-structure/composables) by Nuxt 3.

## `useStrapi`

Depending on which version you have in your [options](/setup#options), you will be using either the v3 or v4 client.
@@ -17,7 +16,6 @@ Note that v3 exposes the same methods with different options. Check out specific

> Learn how to handle Strapi errors globally by using [nuxt hooks](/advanced#errors-handling).

::alert{type="warning"}
All examples below are demonstrated with http calls in script setup. However, to handle SSR properly you may want to use [useAsyncData](/advanced#async-data).
::
@@ -33,8 +31,7 @@ const { find } = useStrapi<Course>()
findOne('courses', '123')
```

If you prefer not to use a default data type and want or want to overide the default, you can pass the data model on individual
methods as well.
If you prefer not to use a default data type and want to override the default, you can pass the data model on individual methods as well.

```ts
const { find } = useStrapi<Course>()
@@ -70,7 +67,7 @@ Returns an entry by `id`.

- **Arguments:**
- contentType: `string`
- id: `string | number`
- id?: `string | number | Strapi4RequestParams`
- params?: [`Strapi4RequestParams`](https://github.com/nuxt-community/strapi-module/blob/dev/src/runtime/types/v4.d.ts#L24)
- **Returns:** `Promise<T>`

@@ -85,6 +82,10 @@ const response = await findOne<Restaurant>('restaurants', route.params.id)
</script>
```

::alert{type="info"}
This method can be used to get a single type entry as `id` is optional. You can pass the `params` instead of the `id` if needed.
::

> Check out the Strapi [Get an entry](https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html#get-an-entry) REST API endpoint.
### `create`
@@ -133,6 +134,10 @@ const onSubmit = async () => {
</script>
```

::alert{type="info"}
This method can be used to update/create a single type entry as you can pass the `data` instead of the `id`.
::

> Check out the Strapi [Update an entry](https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html#update-an-entry) REST API endpoint.
### `delete`
@@ -158,6 +163,10 @@ const onSubmit = async () => {
</script>
```

::alert{type="info"}
This method can be used to delete a single type entry as `id` is optional.
::

> Check out the Strapi [Delete an entry](https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html#delete-an-entry) REST API endpoint.
### `count`
2 changes: 1 addition & 1 deletion src/runtime/composables-v3/useStrapi.ts
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import { useStrapi3 } from '#imports'
interface StrapiV3Client<T> {
count(contentType: string, params?: Strapi3RequestParams): Promise<number>
find<F = T[]>(contentType: string, params?: Strapi3RequestParams): Promise<F>
findOne<F = T>(contentType: string, id: string | number, params?: Strapi3RequestParams): Promise<F>
findOne<F = T>(contentType: string, id?: string | number | Strapi3RequestParams, params?: Strapi3RequestParams): Promise<F>
create<F = T>(contentType: string, data: Partial<F>): Promise<F>
update<F = T>(contentType: string, id: string | number | Partial<F>, data?: Partial<F>): Promise<F>
delete<F = T>(contentType: string, id?: string | number): Promise<F>
13 changes: 11 additions & 2 deletions src/runtime/composables-v3/useStrapi3.ts
Original file line number Diff line number Diff line change
@@ -42,8 +42,16 @@ export const useStrapi3 = () => {
* @param {Strapi3RequestParams} params? - Query parameters
* @returns Promise<T>
*/
const findOne = <T>(contentType: string, id: string | number, params?: Strapi3RequestParams): Promise<T> => {
return client(`/${contentType}/${id}`, { method: 'GET', params })
const findOne = <T>(contentType: string, id?: string | number | Strapi3RequestParams, params?: Strapi3RequestParams): Promise<T> => {
if (typeof id === 'object') {
params = id
// @ts-ignore
id = undefined
}

const path = [contentType, id].filter(Boolean).join('/')

return client(path, { method: 'GET', params })
}

/**
@@ -68,6 +76,7 @@ export const useStrapi3 = () => {
const update = <T>(contentType: string, id: string | number | Partial<T>, data?: Partial<T>): Promise<T> => {
if (typeof id === 'object') {
data = id
// @ts-ignore
id = undefined
}

2 changes: 1 addition & 1 deletion src/runtime/composables-v4/useStrapi.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { useStrapi4 } from '#imports'

interface StrapiV4Client<T> {
find<F = T>(contentType: string, params?: Strapi4RequestParams): Promise<Strapi4ResponseMany<F>>
findOne<F = T>(contentType: string, id: string | number, params?: Strapi4RequestParams): Promise<Strapi4ResponseSingle<F>>
findOne<F = T>(contentType: string, id?: string | number | Strapi4RequestParams, params?: Strapi4RequestParams): Promise<Strapi4ResponseSingle<F>>
create<F = T>(contentType: string, data: Partial<F>): Promise<Strapi4ResponseSingle<F>>
update<F = T>(contentType: string, id: string | number | Partial<F>, data?: Partial<F>): Promise<Strapi4ResponseSingle<F>>
delete<F = T>(contentType: string, id?: string | number): Promise<Strapi4ResponseSingle<F>>
12 changes: 10 additions & 2 deletions src/runtime/composables-v4/useStrapi4.ts
Original file line number Diff line number Diff line change
@@ -31,8 +31,16 @@ export const useStrapi4 = () => {
* @param {Strapi4RequestParams} params? - Query parameters
* @returns Promise<T>
*/
const findOne = <T>(contentType: string, id: string | number, params?: Strapi4RequestParams): Promise<T> => {
return client(`/${contentType}/${id}`, { method: 'GET', params })
const findOne = <T>(contentType: string, id?: string | number | Strapi4RequestParams, params?: Strapi4RequestParams): Promise<T> => {
if (typeof id === 'object') {
params = id
// @ts-ignore
id = undefined
}

const path = [contentType, id].filter(Boolean).join('/')

return client(path, { method: 'GET', params })
}

/**

0 comments on commit ca5b358

Please sign in to comment.