11import type { Strapi3RequestParams } from '../types/v3'
2- // @ts -expect-error this import is not available in stubbed version
3- import { useStrapi3 } from '#imports'
2+ import { useStrapiClient } from '#imports'
43
54interface StrapiV3Client < T > {
65 count ( contentType : string , params ?: Strapi3RequestParams ) : Promise < number >
@@ -12,5 +11,98 @@ interface StrapiV3Client<T> {
1211}
1312
1413export const useStrapi = < T > ( ) : StrapiV3Client < T > => {
15- return useStrapi3 ( )
14+ const client = useStrapiClient ( )
15+
16+ /**
17+ * Count {content-type} entries
18+ *
19+ * @param {string } contentType - Content type's name pluralized
20+ * @param {Strapi3RequestParams } [params] - Query parameters
21+ * @returns Promise<number>
22+ */
23+ const count = ( contentType : string , params ?: Strapi3RequestParams ) : Promise < number > => {
24+ return client ( `/${ contentType } /count` , { method : 'GET' , params } )
25+ }
26+
27+ /**
28+ * Get a list of {content-type} entries
29+ *
30+ * @param {string } contentType - Content type's name pluralized
31+ * @param {Strapi3RequestParams } [params] - Query parameters
32+ * @returns Promise<T>
33+ */
34+ const find = < T > ( contentType : string , params ?: Strapi3RequestParams ) : Promise < T > => {
35+ return client ( `/${ contentType } ` , { method : 'GET' , params } )
36+ }
37+
38+ /**
39+ * Get a specific {content-type} entry
40+ *
41+ * @param {string } contentType - Content type's name pluralized
42+ * @param {string|number } id - ID of entry
43+ * @param {Strapi3RequestParams } [params] - Query parameters
44+ * @returns Promise<T>
45+ */
46+ const findOne = < T > ( contentType : string , id ?: string | number | Strapi3RequestParams , params ?: Strapi3RequestParams ) : Promise < T > => {
47+ if ( typeof id === 'object' ) {
48+ params = id
49+ id = undefined
50+ }
51+
52+ const path = [ contentType , id ] . filter ( Boolean ) . join ( '/' )
53+
54+ return client ( path , { method : 'GET' , params } )
55+ }
56+
57+ /**
58+ * Create a {content-type} entry
59+ *
60+ * @param {string } contentType - Content type's name pluralized
61+ * @param {Record<string, any> } data - Form data
62+ * @returns Promise<T>
63+ */
64+ const create = < T > ( contentType : string , data : Partial < T > ) : Promise < T > => {
65+ return client ( `/${ contentType } ` , { method : 'POST' , body : data } )
66+ }
67+
68+ /**
69+ * Update an entry
70+ *
71+ * @param {string } contentType - Content type's name pluralized
72+ * @param {string|number } id - ID of entry to be updated
73+ * @param {Record<string, any> } data - Form data
74+ * @returns Promise<T>
75+ */
76+ const update = < T > ( contentType : string , id : string | number | Partial < T > , data ?: Partial < T > ) : Promise < T > => {
77+ if ( typeof id === 'object' ) {
78+ data = id
79+ id = undefined
80+ }
81+
82+ const path = [ contentType , id ] . filter ( Boolean ) . join ( '/' )
83+
84+ return client ( path , { method : 'PUT' , body : data } )
85+ }
86+
87+ /**
88+ * Delete an entry
89+ *
90+ * @param {string } contentType - Content type's name pluralized
91+ * @param {string|number } id - ID of entry to be deleted
92+ * @returns Promise<T>
93+ */
94+ const _delete = < T > ( contentType : string , id ?: string | number ) : Promise < T > => {
95+ const path = [ contentType , id ] . filter ( Boolean ) . join ( '/' )
96+
97+ return client ( path , { method : 'DELETE' } )
98+ }
99+
100+ return {
101+ count,
102+ find,
103+ findOne,
104+ create,
105+ update,
106+ delete : _delete
107+ }
16108}
0 commit comments