diff --git a/.gitignore b/.gitignore index 393cb501..1f5e0116 100755 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ sw.js .output .env .history + +package-lock.json diff --git a/src/module.ts b/src/module.ts index 079a5d02..228fdbf4 100644 --- a/src/module.ts +++ b/src/module.ts @@ -39,7 +39,7 @@ export interface ModuleOptions { * @type string * @example 'v3' */ - version?: 'v4' | 'v3' + version?: 'v5' | 'v4' | 'v3' /** * Nuxt Cookie Options diff --git a/src/runtime/composables-v5/useStrapi.ts b/src/runtime/composables-v5/useStrapi.ts new file mode 100644 index 00000000..708ea554 --- /dev/null +++ b/src/runtime/composables-v5/useStrapi.ts @@ -0,0 +1,97 @@ +import type { FetchOptions } from 'ofetch' + +import type { Strapi5ResponseSingle, Strapi5RequestParams, Strapi5ResponseMany } from '../types/v5' +import { useStrapiClient } from '#imports' + +interface StrapiV5Client { + find(contentType: string, params?: Strapi5RequestParams): Promise> + findOne(contentType: string, documentId?: string | Strapi5RequestParams, params?: Strapi5RequestParams): Promise> + create(contentType: string, data: Partial): Promise> + update(contentType: string, documentId: string | Partial, data?: Partial): Promise> + delete(contentType: string, documentId?: string): Promise> +} + +export const useStrapi = (): StrapiV5Client => { + const client = useStrapiClient() + + /** + * Get a list of {content-type} entries + * + * @param {string} contentType - Content type's name pluralized + * @param {Strapi5RequestParams} [params] - Query parameters + * @returns Promise + */ + const find = (contentType: string, params?: Strapi5RequestParams, fetchOptions?: FetchOptions): Promise> => { + return client(`/${contentType}`, { method: 'GET', params, ...fetchOptions }) + } + + /** + * Get a specific {content-type} entry + * + * @param {string} contentType - Content type's name pluralized + * @param {string} documentId - ID of entry + * @param {Strapi5RequestParams} [params] - Query parameters + * @returns Promise + */ + const findOne = (contentType: string, documentId?: string | Strapi5RequestParams, params?: Strapi5RequestParams, fetchOptions?: FetchOptions): Promise> => { + if (typeof documentId === 'object') { + params = documentId + documentId = undefined + } + + const path = [contentType, documentId].filter(Boolean).join('/') + + return client(path, { method: 'GET', params, ...fetchOptions }) + } + + /** + * Create a {content-type} entry + * + * @param {string} contentType - Content type's name pluralized + * @param {Record} data - Form data + * @returns Promise + */ + const create = (contentType: string, data: Partial): Promise> => { + return client(`/${contentType}`, { method: 'POST', body: { data } }) + } + + /** + * Update an entry + * + * @param {string} contentType - Content type's name pluralized + * @param {string} documentId - ID of entry to be updated + * @param {Record} data - Form data + * @returns Promise + */ + const update = (contentType: string, documentId: string | Partial, data?: Partial): Promise> => { + if (typeof documentId === 'object') { + data = documentId + documentId = undefined + } + + const path = [contentType, documentId].filter(Boolean).join('/') + + return client(path, { method: 'PUT', body: { data } }) + } + + /** + * Delete an entry + * + * @param {string} contentType - Content type's name pluralized + * @param {string|number} id - ID of entry to be deleted + * @returns Promise + */ + const _delete = (contentType: string, id?: string | number): Promise => { + const path = [contentType, id].filter(Boolean).join('/') + + return client(path, { method: 'DELETE' }) + } + + return { + find, + findOne, + create, + update, + delete: _delete + } +} diff --git a/src/runtime/types/index.ts b/src/runtime/types/index.ts index af1f81ca..57a49876 100644 --- a/src/runtime/types/index.ts +++ b/src/runtime/types/index.ts @@ -572,3 +572,4 @@ export interface StrapiGraphqlVariables { export * from './v3' export * from './v4' +export * from './v5' diff --git a/src/runtime/types/v5.ts b/src/runtime/types/v5.ts new file mode 100644 index 00000000..e20a53bb --- /dev/null +++ b/src/runtime/types/v5.ts @@ -0,0 +1,42 @@ +import type { MetaResponsePaginationByOffset, MetaResponsePaginationByPage, PaginationByOffset, PaginationByPage, StrapiLocale } from '.' + +export interface Strapi5Error { + error: { + status: number + name: string + message: string + details: Record + } +} + +export interface Strapi5RequestParams { + fields?: Array + populate?: string | Array | object + sort?: string | Array + pagination?: PaginationByOffset | PaginationByPage + filters?: Record + publicationState?: 'live' | 'preview' + locale?: StrapiLocale +} + +export interface Strapi5SystemFields { + documentId: string + locale?: StrapiLocale +} + +export type Strapi5ResponseData = Strapi5SystemFields & T + +export interface Strapi5ResponseSingle { + data: Strapi5ResponseData + meta: Strapi5ResponseMeta +} + +export interface Strapi5ResponseMany { + data: Strapi5ResponseData[] + meta: Strapi5ResponseMeta +} + +export interface Strapi5ResponseMeta { + pagination: MetaResponsePaginationByPage | MetaResponsePaginationByOffset + [key: string]: unknown +}