Skip to content

Commit

Permalink
fix(composables): handle single types
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincanac committed Jan 19, 2023
1 parent 41e0be0 commit e26e433
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
21 changes: 15 additions & 6 deletions docs/content/3.usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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).
::
Expand All @@ -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>()
Expand Down Expand Up @@ -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>`

Expand All @@ -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.
::

> 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`
Expand Down Expand Up @@ -116,7 +117,7 @@ Partially updates an entry by `id` and returns its value. Fields that aren't sen

- **Arguments:**
- contentType: `string`
- id: `string | number | Partial<T>`
- id?: `string | number | Partial<T>`
- data?: `Partial<T>`
- **Returns:** `Promise<T>`

Expand All @@ -133,6 +134,10 @@ const onSubmit = async () => {
</script>
```

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

> 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`
Expand All @@ -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`
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/composables-v3/useStrapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ 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>
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>
}

Expand Down
15 changes: 12 additions & 3 deletions src/runtime/composables-v3/useStrapi3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}

/**
Expand All @@ -65,9 +73,10 @@ export const useStrapi3 = () => {
* @param {Record<string, any>} data - Form data
* @returns Promise<T>
*/
const update = <T>(contentType: string, id: string | number | Partial<T>, data?: Partial<T>): Promise<T> => {
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
}

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/composables-v4/useStrapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ 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>>
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>>
}

Expand Down
14 changes: 11 additions & 3 deletions src/runtime/composables-v4/useStrapi4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}

/**
Expand All @@ -54,7 +62,7 @@ export const useStrapi4 = () => {
* @param {Record<string, any>} data - Form data
* @returns Promise<T>
*/
const update = <T>(contentType: string, id: string | number | Partial<T>, data?: Partial<T>): Promise<T> => {
const update = <T>(contentType: string, id?: string | number | Partial<T>, data?: Partial<T>): Promise<T> => {
if (typeof id === 'object') {
data = id
// @ts-ignore
Expand Down

0 comments on commit e26e433

Please sign in to comment.