diff --git a/openapi-ts-request.config.ts b/openapi-ts-request.config.ts index 05195d1..e24d862 100644 --- a/openapi-ts-request.config.ts +++ b/openapi-ts-request.config.ts @@ -4,7 +4,7 @@ export default [ { schemaPath: 'http://petstore.swagger.io/v2/swagger.json', serversPath: './src/service/app', - requestLibPath: `import request from '@/utils/request';\n import { CustomRequestOptions } from '@/http/interceptor';`, + requestLibPath: `import request from '@/http/vue-query';\n import { CustomRequestOptions } from '@/http/interceptor';`, requestOptionsType: 'CustomRequestOptions', isGenReactQuery: true, reactQueryMode: 'vue', diff --git a/src/api/foo-alova.ts b/src/api/foo-alova.ts index e33a17c..de35095 100644 --- a/src/api/foo-alova.ts +++ b/src/api/foo-alova.ts @@ -1,4 +1,4 @@ -import { API_DOMAINS, http } from '@/http/request/alova' +import { API_DOMAINS, http } from '@/http/alova' export interface IFoo { id: number diff --git a/src/service/index/vue-query.ts b/src/api/foo-vue-query.ts similarity index 100% rename from src/service/index/vue-query.ts rename to src/api/foo-vue-query.ts diff --git a/src/api/foo.ts b/src/api/foo.ts index 51a1905..cd8b188 100644 --- a/src/api/foo.ts +++ b/src/api/foo.ts @@ -14,3 +14,30 @@ export function foo() { }, }) } + +export interface IFooItem { + id: string + name: string +} + +/** GET 请求 */ +export function getFooAPI(name: string) { + return http.get('/foo', { name }) +} +/** GET 请求;支持 传递 header 的范例 */ +export function getFooAPI2(name: string) { + return http.get('/foo', { name }, { 'Content-Type-100': '100' }) +} + +/** POST 请求 */ +export function postFooAPI(name: string) { + return http.post('/foo', { name }) +} +/** POST 请求;需要传递 query 参数的范例;微信小程序经常有同时需要query参数和body参数的场景 */ +export function postFooAPI2(name: string) { + return http.post('/foo', { name }) +} +/** POST 请求;支持 传递 header 的范例 */ +export function postFooAPI3(name: string) { + return http.post('/foo', { name }, { name }, { 'Content-Type-100': '100' }) +} diff --git a/src/http/README.md b/src/http/README.md index 9d04585..bbacd1c 100644 --- a/src/http/README.md +++ b/src/http/README.md @@ -2,8 +2,8 @@ 目前unibest支持3种请求库: - 菲鸽简单封装的 `简单版本http`,路径(src/http/http.ts),对应的示例在 src/api/foo.ts -- `alova 的 http`,路径(src/http/request/alova.ts),对应的示例在 src/api/foo-alova.ts -- `vue-query`, 路径(src/utils/request.ts), 目前主要用在自动生成接口,详情看(https://unibest.tech/base/17-generate),示例在 src/service/app 文件夹 +- `alova 的 http`,路径(src/http/alova.ts),对应的示例在 src/api/foo-alova.ts +- `vue-query`, 路径(src/http/vue-query.ts), 目前主要用在自动生成接口,详情看(https://unibest.tech/base/17-generate),示例在 src/service/app 文件夹 ## 如何选择 如果您以前用过 alova 或者 vue-query,可以优先使用您熟悉的。 diff --git a/src/http/request/alova.ts b/src/http/alova.ts similarity index 96% rename from src/http/request/alova.ts rename to src/http/alova.ts index 039326d..ba15bbd 100644 --- a/src/http/request/alova.ts +++ b/src/http/alova.ts @@ -1,11 +1,11 @@ import type { uniappRequestAdapter } from '@alova/adapter-uniapp' -import type { IResponse } from './types' +import type { IResponse } from './tools/types' import AdapterUniapp from '@alova/adapter-uniapp' import { createAlova } from 'alova' import { createServerTokenAuthentication } from 'alova/client' import VueHook from 'alova/vue' import { toast } from '@/utils/toast' -import { ContentTypeEnum, ResultEnum, ShowMessage } from './enum' +import { ContentTypeEnum, ResultEnum, ShowMessage } from './tools/enum' // 配置动态Tag export const API_DOMAINS = { diff --git a/src/http/interceptor.ts b/src/http/interceptor.ts index 8c99a22..fad6ca6 100644 --- a/src/http/interceptor.ts +++ b/src/http/interceptor.ts @@ -1,7 +1,7 @@ import { useUserStore } from '@/store' import { getEnvBaseUrl } from '@/utils' import { platform } from '@/utils/platform' -import { stringifyQuery } from './queryString' +import { stringifyQuery } from './tools/queryString' export type CustomRequestOptions = UniApp.RequestOptions & { query?: Record diff --git a/src/http/request/enum.ts b/src/http/tools/enum.ts similarity index 100% rename from src/http/request/enum.ts rename to src/http/tools/enum.ts diff --git a/src/http/queryString.ts b/src/http/tools/queryString.ts similarity index 100% rename from src/http/queryString.ts rename to src/http/tools/queryString.ts diff --git a/src/http/request/types.ts b/src/http/tools/types.ts similarity index 100% rename from src/http/request/types.ts rename to src/http/tools/types.ts diff --git a/src/http/vue-query.ts b/src/http/vue-query.ts new file mode 100644 index 0000000..72bbe2a --- /dev/null +++ b/src/http/vue-query.ts @@ -0,0 +1,30 @@ +import type { CustomRequestOptions } from '@/http/interceptor' +import { http } from './http' + +/* + * openapi-ts-request 工具的 request 跨客户端适配方法 + */ +export default function request( + url: string, + options: Omit & { + params?: Record + headers?: Record + }, +) { + const requestOptions = { + url, + ...options, + } + + if (options.params) { + requestOptions.query = requestOptions.params + delete requestOptions.params + } + + if (options.headers) { + requestOptions.header = options.headers + delete requestOptions.headers + } + + return http(requestOptions) +} diff --git a/src/service/app/displayEnumLabel.ts b/src/service/app/displayEnumLabel.ts deleted file mode 100644 index 4974815..0000000 --- a/src/service/app/displayEnumLabel.ts +++ /dev/null @@ -1,13 +0,0 @@ -/* eslint-disable */ -// @ts-ignore -import * as API from './types'; - -export function displayStatusEnum(field: API.IStatusEnum) { - return { available: 'available', pending: 'pending', sold: 'sold' }[field]; -} - -export function displayStatusEnum2(field: API.IStatusEnum2) { - return { placed: 'placed', approved: 'approved', delivered: 'delivered' }[ - field - ]; -} diff --git a/src/service/app/index.ts b/src/service/app/index.ts deleted file mode 100644 index 45b6e53..0000000 --- a/src/service/app/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -/* eslint-disable */ -// @ts-ignore -export * from './types'; -export * from './displayEnumLabel'; - -export * from './pet'; -export * from './pet.vuequery'; -export * from './store'; -export * from './store.vuequery'; -export * from './user'; -export * from './user.vuequery'; diff --git a/src/service/app/pet.ts b/src/service/app/pet.ts deleted file mode 100644 index 1960682..0000000 --- a/src/service/app/pet.ts +++ /dev/null @@ -1,193 +0,0 @@ -/* eslint-disable */ -// @ts-ignore -import request from '@/utils/request'; -import { CustomRequestOptions } from '@/http/interceptor'; - -import * as API from './types'; - -/** Update an existing pet PUT /pet */ -export async function updatePet({ - body, - options, -}: { - body: API.Pet; - options?: CustomRequestOptions; -}) { - return request('/pet', { - method: 'PUT', - headers: { - 'Content-Type': 'application/json', - }, - data: body, - ...(options || {}), - }); -} - -/** Add a new pet to the store POST /pet */ -export async function addPet({ - body, - options, -}: { - body: API.Pet; - options?: CustomRequestOptions; -}) { - return request('/pet', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - data: body, - ...(options || {}), - }); -} - -/** Find pet by ID Returns a single pet GET /pet/${param0} */ -export async function getPetById({ - params, - options, -}: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.getPetByIdParams; - options?: CustomRequestOptions; -}) { - const { petId: param0, ...queryParams } = params; - - return request(`/pet/${param0}`, { - method: 'GET', - params: { ...queryParams }, - ...(options || {}), - }); -} - -/** Updates a pet in the store with form data POST /pet/${param0} */ -export async function updatePetWithForm({ - params, - body, - options, -}: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.updatePetWithFormParams; - body: { - /** Updated name of the pet */ - name?: string; - /** Updated status of the pet */ - status?: string; - }; - options?: CustomRequestOptions; -}) { - const { petId: param0, ...queryParams } = params; - - return request(`/pet/${param0}`, { - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - }, - params: { ...queryParams }, - data: body, - ...(options || {}), - }); -} - -/** Deletes a pet DELETE /pet/${param0} */ -export async function deletePet({ - params, - options, -}: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.deletePetParams; - options?: CustomRequestOptions; -}) { - const { petId: param0, ...queryParams } = params; - - return request(`/pet/${param0}`, { - method: 'DELETE', - params: { ...queryParams }, - ...(options || {}), - }); -} - -/** uploads an image POST /pet/${param0}/uploadImage */ -export async function uploadFile({ - params, - body, - file, - options, -}: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.uploadFileParams; - body: { - /** Additional data to pass to server */ - additionalMetadata?: string; - }; - file?: File; - options?: CustomRequestOptions; -}) { - const { petId: param0, ...queryParams } = params; - const formData = new FormData(); - - if (file) { - formData.append('file', file); - } - - Object.keys(body).forEach((ele) => { - const item = (body as { [key: string]: any })[ele]; - - if (item !== undefined && item !== null) { - if (typeof item === 'object' && !(item instanceof File)) { - if (item instanceof Array) { - item.forEach((f) => formData.append(ele, f || '')); - } else { - formData.append(ele, JSON.stringify(item)); - } - } else { - formData.append(ele, item); - } - } - }); - - return request(`/pet/${param0}/uploadImage`, { - method: 'POST', - headers: { - 'Content-Type': 'multipart/form-data', - }, - params: { ...queryParams }, - data: formData, - ...(options || {}), - }); -} - -/** Finds Pets by status Multiple status values can be provided with comma separated strings GET /pet/findByStatus */ -export async function findPetsByStatus({ - params, - options, -}: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.findPetsByStatusParams; - options?: CustomRequestOptions; -}) { - return request('/pet/findByStatus', { - method: 'GET', - params: { - ...params, - }, - ...(options || {}), - }); -} - -/** Finds Pets by tags Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. GET /pet/findByTags */ -export async function findPetsByTags({ - params, - options, -}: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.findPetsByTagsParams; - options?: CustomRequestOptions; -}) { - return request('/pet/findByTags', { - method: 'GET', - params: { - ...params, - }, - ...(options || {}), - }); -} diff --git a/src/service/app/pet.vuequery.ts b/src/service/app/pet.vuequery.ts deleted file mode 100644 index 4029b87..0000000 --- a/src/service/app/pet.vuequery.ts +++ /dev/null @@ -1,151 +0,0 @@ -/* eslint-disable */ -// @ts-ignore -import { queryOptions, useMutation } from '@tanstack/vue-query'; -import type { DefaultError } from '@tanstack/vue-query'; -import request from '@/utils/request'; -import { CustomRequestOptions } from '@/http/interceptor'; - -import * as apis from './pet'; -import * as API from './types'; - -/** Update an existing pet PUT /pet */ -export function useUpdatePetMutation(options?: { - onSuccess?: (value?: unknown) => void; - onError?: (error?: DefaultError) => void; -}) { - const { onSuccess, onError } = options || {}; - - const response = useMutation({ - mutationFn: apis.updatePet, - onSuccess(data: unknown) { - onSuccess?.(data); - }, - onError(error) { - onError?.(error); - }, - }); - - return response; -} - -/** Add a new pet to the store POST /pet */ -export function useAddPetMutation(options?: { - onSuccess?: (value?: unknown) => void; - onError?: (error?: DefaultError) => void; -}) { - const { onSuccess, onError } = options || {}; - - const response = useMutation({ - mutationFn: apis.addPet, - onSuccess(data: unknown) { - onSuccess?.(data); - }, - onError(error) { - onError?.(error); - }, - }); - - return response; -} - -/** Find pet by ID Returns a single pet GET /pet/${param0} */ -export function getPetByIdQueryOptions(options: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.getPetByIdParams; - options?: CustomRequestOptions; -}) { - return queryOptions({ - queryFn: async ({ queryKey }) => { - return apis.getPetById(queryKey[1] as typeof options); - }, - queryKey: ['getPetById', options], - }); -} - -/** Updates a pet in the store with form data POST /pet/${param0} */ -export function useUpdatePetWithFormMutation(options?: { - onSuccess?: (value?: unknown) => void; - onError?: (error?: DefaultError) => void; -}) { - const { onSuccess, onError } = options || {}; - - const response = useMutation({ - mutationFn: apis.updatePetWithForm, - onSuccess(data: unknown) { - onSuccess?.(data); - }, - onError(error) { - onError?.(error); - }, - }); - - return response; -} - -/** Deletes a pet DELETE /pet/${param0} */ -export function useDeletePetMutation(options?: { - onSuccess?: (value?: unknown) => void; - onError?: (error?: DefaultError) => void; -}) { - const { onSuccess, onError } = options || {}; - - const response = useMutation({ - mutationFn: apis.deletePet, - onSuccess(data: unknown) { - onSuccess?.(data); - }, - onError(error) { - onError?.(error); - }, - }); - - return response; -} - -/** uploads an image POST /pet/${param0}/uploadImage */ -export function useUploadFileMutation(options?: { - onSuccess?: (value?: API.ApiResponse) => void; - onError?: (error?: DefaultError) => void; -}) { - const { onSuccess, onError } = options || {}; - - const response = useMutation({ - mutationFn: apis.uploadFile, - onSuccess(data: API.ApiResponse) { - onSuccess?.(data); - }, - onError(error) { - onError?.(error); - }, - }); - - return response; -} - -/** Finds Pets by status Multiple status values can be provided with comma separated strings GET /pet/findByStatus */ -export function findPetsByStatusQueryOptions(options: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.findPetsByStatusParams; - options?: CustomRequestOptions; -}) { - return queryOptions({ - queryFn: async ({ queryKey }) => { - return apis.findPetsByStatus(queryKey[1] as typeof options); - }, - queryKey: ['findPetsByStatus', options], - }); -} - -/** Finds Pets by tags Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. GET /pet/findByTags */ -export function findPetsByTagsQueryOptions(options: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.findPetsByTagsParams; - options?: CustomRequestOptions; -}) { - return queryOptions({ - queryFn: async ({ queryKey }) => { - return apis.findPetsByTags(queryKey[1] as typeof options); - }, - queryKey: ['findPetsByTags', options], - }); -} diff --git a/src/service/app/store.ts b/src/service/app/store.ts deleted file mode 100644 index 710bd1c..0000000 --- a/src/service/app/store.ts +++ /dev/null @@ -1,72 +0,0 @@ -/* eslint-disable */ -// @ts-ignore -import request from '@/utils/request'; -import { CustomRequestOptions } from '@/http/interceptor'; - -import * as API from './types'; - -/** Returns pet inventories by status Returns a map of status codes to quantities GET /store/inventory */ -export async function getInventory({ - options, -}: { - options?: CustomRequestOptions; -}) { - return request>('/store/inventory', { - method: 'GET', - ...(options || {}), - }); -} - -/** Place an order for a pet POST /store/order */ -export async function placeOrder({ - body, - options, -}: { - body: API.Order; - options?: CustomRequestOptions; -}) { - return request('/store/order', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - data: body, - ...(options || {}), - }); -} - -/** Find purchase order by ID For valid response try integer IDs with value >= 1 and <= 10. Other values will generated exceptions GET /store/order/${param0} */ -export async function getOrderById({ - params, - options, -}: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.getOrderByIdParams; - options?: CustomRequestOptions; -}) { - const { orderId: param0, ...queryParams } = params; - - return request(`/store/order/${param0}`, { - method: 'GET', - params: { ...queryParams }, - ...(options || {}), - }); -} - -/** Delete purchase order by ID For valid response try integer IDs with positive integer value. Negative or non-integer values will generate API errors DELETE /store/order/${param0} */ -export async function deleteOrder({ - params, - options, -}: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.deleteOrderParams; - options?: CustomRequestOptions; -}) { - const { orderId: param0, ...queryParams } = params; - - return request(`/store/order/${param0}`, { - method: 'DELETE', - params: { ...queryParams }, - ...(options || {}), - }); -} diff --git a/src/service/app/store.vuequery.ts b/src/service/app/store.vuequery.ts deleted file mode 100644 index d1e20bd..0000000 --- a/src/service/app/store.vuequery.ts +++ /dev/null @@ -1,75 +0,0 @@ -/* eslint-disable */ -// @ts-ignore -import { queryOptions, useMutation } from '@tanstack/vue-query'; -import type { DefaultError } from '@tanstack/vue-query'; -import request from '@/utils/request'; -import { CustomRequestOptions } from '@/http/interceptor'; - -import * as apis from './store'; -import * as API from './types'; - -/** Returns pet inventories by status Returns a map of status codes to quantities GET /store/inventory */ -export function getInventoryQueryOptions(options: { - options?: CustomRequestOptions; -}) { - return queryOptions({ - queryFn: async ({ queryKey }) => { - return apis.getInventory(queryKey[1] as typeof options); - }, - queryKey: ['getInventory', options], - }); -} - -/** Place an order for a pet POST /store/order */ -export function usePlaceOrderMutation(options?: { - onSuccess?: (value?: API.Order) => void; - onError?: (error?: DefaultError) => void; -}) { - const { onSuccess, onError } = options || {}; - - const response = useMutation({ - mutationFn: apis.placeOrder, - onSuccess(data: API.Order) { - onSuccess?.(data); - }, - onError(error) { - onError?.(error); - }, - }); - - return response; -} - -/** Find purchase order by ID For valid response try integer IDs with value >= 1 and <= 10. Other values will generated exceptions GET /store/order/${param0} */ -export function getOrderByIdQueryOptions(options: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.getOrderByIdParams; - options?: CustomRequestOptions; -}) { - return queryOptions({ - queryFn: async ({ queryKey }) => { - return apis.getOrderById(queryKey[1] as typeof options); - }, - queryKey: ['getOrderById', options], - }); -} - -/** Delete purchase order by ID For valid response try integer IDs with positive integer value. Negative or non-integer values will generate API errors DELETE /store/order/${param0} */ -export function useDeleteOrderMutation(options?: { - onSuccess?: (value?: unknown) => void; - onError?: (error?: DefaultError) => void; -}) { - const { onSuccess, onError } = options || {}; - - const response = useMutation({ - mutationFn: apis.deleteOrder, - onSuccess(data: unknown) { - onSuccess?.(data); - }, - onError(error) { - onError?.(error); - }, - }); - - return response; -} diff --git a/src/service/app/types.ts b/src/service/app/types.ts deleted file mode 100644 index 4691b64..0000000 --- a/src/service/app/types.ts +++ /dev/null @@ -1,128 +0,0 @@ -/* eslint-disable */ -// @ts-ignore - -export type ApiResponse = { - code?: number; - type?: string; - message?: string; -}; - -export type Category = { - id?: number; - name?: string; -}; - -export type deleteOrderParams = { - /** ID of the order that needs to be deleted */ - orderId: number; -}; - -export type deletePetParams = { - /** Pet id to delete */ - petId: number; -}; - -export type deleteUserParams = { - /** The name that needs to be deleted */ - username: string; -}; - -export type findPetsByStatusParams = { - /** Status values that need to be considered for filter */ - status: ('available' | 'pending' | 'sold')[]; -}; - -export type findPetsByTagsParams = { - /** Tags to filter by */ - tags: string[]; -}; - -export type getOrderByIdParams = { - /** ID of pet that needs to be fetched */ - orderId: number; -}; - -export type getPetByIdParams = { - /** ID of pet to return */ - petId: number; -}; - -export type getUserByNameParams = { - /** The name that needs to be fetched. Use user1 for testing. */ - username: string; -}; - -export type loginUserParams = { - /** The user name for login */ - username: string; - /** The password for login in clear text */ - password: string; -}; - -export type Order = { - id?: number; - petId?: number; - quantity?: number; - shipDate?: string; - /** Order Status */ - status?: 'placed' | 'approved' | 'delivered'; - complete?: boolean; -}; - -export type Pet = { - id?: number; - category?: Category; - name: string; - photoUrls: string[]; - tags?: Tag[]; - /** pet status in the store */ - status?: 'available' | 'pending' | 'sold'; -}; - -export enum StatusEnum { - available = 'available', - pending = 'pending', - sold = 'sold', -} - -export type IStatusEnum = keyof typeof StatusEnum; - -export enum StatusEnum2 { - placed = 'placed', - approved = 'approved', - delivered = 'delivered', -} - -export type IStatusEnum2 = keyof typeof StatusEnum2; - -export type Tag = { - id?: number; - name?: string; -}; - -export type updatePetWithFormParams = { - /** ID of pet that needs to be updated */ - petId: number; -}; - -export type updateUserParams = { - /** name that need to be updated */ - username: string; -}; - -export type uploadFileParams = { - /** ID of pet to update */ - petId: number; -}; - -export type User = { - id?: number; - username?: string; - firstName?: string; - lastName?: string; - email?: string; - password?: string; - phone?: string; - /** User Status */ - userStatus?: number; -}; diff --git a/src/service/app/user.ts b/src/service/app/user.ts deleted file mode 100644 index bb39677..0000000 --- a/src/service/app/user.ts +++ /dev/null @@ -1,150 +0,0 @@ -/* eslint-disable */ -// @ts-ignore -import request from '@/utils/request'; -import { CustomRequestOptions } from '@/http/interceptor'; - -import * as API from './types'; - -/** Create user This can only be done by the logged in user. 返回值: successful operation POST /user */ -export async function createUser({ - body, - options, -}: { - body: API.User; - options?: CustomRequestOptions; -}) { - return request('/user', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - data: body, - ...(options || {}), - }); -} - -/** Get user by user name GET /user/${param0} */ -export async function getUserByName({ - params, - options, -}: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.getUserByNameParams; - options?: CustomRequestOptions; -}) { - const { username: param0, ...queryParams } = params; - - return request(`/user/${param0}`, { - method: 'GET', - params: { ...queryParams }, - ...(options || {}), - }); -} - -/** Updated user This can only be done by the logged in user. PUT /user/${param0} */ -export async function updateUser({ - params, - body, - options, -}: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.updateUserParams; - body: API.User; - options?: CustomRequestOptions; -}) { - const { username: param0, ...queryParams } = params; - - return request(`/user/${param0}`, { - method: 'PUT', - headers: { - 'Content-Type': 'application/json', - }, - params: { ...queryParams }, - data: body, - ...(options || {}), - }); -} - -/** Delete user This can only be done by the logged in user. DELETE /user/${param0} */ -export async function deleteUser({ - params, - options, -}: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.deleteUserParams; - options?: CustomRequestOptions; -}) { - const { username: param0, ...queryParams } = params; - - return request(`/user/${param0}`, { - method: 'DELETE', - params: { ...queryParams }, - ...(options || {}), - }); -} - -/** Creates list of users with given input array 返回值: successful operation POST /user/createWithArray */ -export async function createUsersWithArrayInput({ - body, - options, -}: { - body: API.User[]; - options?: CustomRequestOptions; -}) { - return request('/user/createWithArray', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - data: body, - ...(options || {}), - }); -} - -/** Creates list of users with given input array 返回值: successful operation POST /user/createWithList */ -export async function createUsersWithListInput({ - body, - options, -}: { - body: API.User[]; - options?: CustomRequestOptions; -}) { - return request('/user/createWithList', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - data: body, - ...(options || {}), - }); -} - -/** Logs user into the system GET /user/login */ -export async function loginUser({ - params, - options, -}: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.loginUserParams; - options?: CustomRequestOptions; -}) { - return request('/user/login', { - method: 'GET', - params: { - ...params, - }, - ...(options || {}), - }); -} - -/** Logs out current logged in user session 返回值: successful operation GET /user/logout */ -export async function logoutUser({ - options, -}: { - options?: CustomRequestOptions; -}) { - return request('/user/logout', { - method: 'GET', - ...(options || {}), - }); -} diff --git a/src/service/app/user.vuequery.ts b/src/service/app/user.vuequery.ts deleted file mode 100644 index 79d2c57..0000000 --- a/src/service/app/user.vuequery.ts +++ /dev/null @@ -1,149 +0,0 @@ -/* eslint-disable */ -// @ts-ignore -import { queryOptions, useMutation } from '@tanstack/vue-query'; -import type { DefaultError } from '@tanstack/vue-query'; -import request from '@/utils/request'; -import { CustomRequestOptions } from '@/http/interceptor'; - -import * as apis from './user'; -import * as API from './types'; - -/** Create user This can only be done by the logged in user. 返回值: successful operation POST /user */ -export function useCreateUserMutation(options?: { - onSuccess?: (value?: unknown) => void; - onError?: (error?: DefaultError) => void; -}) { - const { onSuccess, onError } = options || {}; - - const response = useMutation({ - mutationFn: apis.createUser, - onSuccess(data: unknown) { - onSuccess?.(data); - }, - onError(error) { - onError?.(error); - }, - }); - - return response; -} - -/** Get user by user name GET /user/${param0} */ -export function getUserByNameQueryOptions(options: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.getUserByNameParams; - options?: CustomRequestOptions; -}) { - return queryOptions({ - queryFn: async ({ queryKey }) => { - return apis.getUserByName(queryKey[1] as typeof options); - }, - queryKey: ['getUserByName', options], - }); -} - -/** Updated user This can only be done by the logged in user. PUT /user/${param0} */ -export function useUpdateUserMutation(options?: { - onSuccess?: (value?: unknown) => void; - onError?: (error?: DefaultError) => void; -}) { - const { onSuccess, onError } = options || {}; - - const response = useMutation({ - mutationFn: apis.updateUser, - onSuccess(data: unknown) { - onSuccess?.(data); - }, - onError(error) { - onError?.(error); - }, - }); - - return response; -} - -/** Delete user This can only be done by the logged in user. DELETE /user/${param0} */ -export function useDeleteUserMutation(options?: { - onSuccess?: (value?: unknown) => void; - onError?: (error?: DefaultError) => void; -}) { - const { onSuccess, onError } = options || {}; - - const response = useMutation({ - mutationFn: apis.deleteUser, - onSuccess(data: unknown) { - onSuccess?.(data); - }, - onError(error) { - onError?.(error); - }, - }); - - return response; -} - -/** Creates list of users with given input array 返回值: successful operation POST /user/createWithArray */ -export function useCreateUsersWithArrayInputMutation(options?: { - onSuccess?: (value?: unknown) => void; - onError?: (error?: DefaultError) => void; -}) { - const { onSuccess, onError } = options || {}; - - const response = useMutation({ - mutationFn: apis.createUsersWithArrayInput, - onSuccess(data: unknown) { - onSuccess?.(data); - }, - onError(error) { - onError?.(error); - }, - }); - - return response; -} - -/** Creates list of users with given input array 返回值: successful operation POST /user/createWithList */ -export function useCreateUsersWithListInputMutation(options?: { - onSuccess?: (value?: unknown) => void; - onError?: (error?: DefaultError) => void; -}) { - const { onSuccess, onError } = options || {}; - - const response = useMutation({ - mutationFn: apis.createUsersWithListInput, - onSuccess(data: unknown) { - onSuccess?.(data); - }, - onError(error) { - onError?.(error); - }, - }); - - return response; -} - -/** Logs user into the system GET /user/login */ -export function loginUserQueryOptions(options: { - // 叠加生成的Param类型 (非body参数openapi默认没有生成对象) - params: API.loginUserParams; - options?: CustomRequestOptions; -}) { - return queryOptions({ - queryFn: async ({ queryKey }) => { - return apis.loginUser(queryKey[1] as typeof options); - }, - queryKey: ['loginUser', options], - }); -} - -/** Logs out current logged in user session 返回值: successful operation GET /user/logout */ -export function logoutUserQueryOptions(options: { - options?: CustomRequestOptions; -}) { - return queryOptions({ - queryFn: async ({ queryKey }) => { - return apis.logoutUser(queryKey[1] as typeof options); - }, - queryKey: ['logoutUser', options], - }); -} diff --git a/src/service/index/foo.ts b/src/service/index/foo.ts deleted file mode 100644 index 56ecac4..0000000 --- a/src/service/index/foo.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { http } from '@/http/http' - -export interface IFooItem { - id: string - name: string -} - -/** GET 请求 */ -export function getFooAPI(name: string) { - return http.get('/foo', { name }) -} -/** GET 请求;支持 传递 header 的范例 */ -export function getFooAPI2(name: string) { - return http.get('/foo', { name }, { 'Content-Type-100': '100' }) -} - -/** POST 请求 */ -export function postFooAPI(name: string) { - return http.post('/foo', { name }) -} -/** POST 请求;需要传递 query 参数的范例;微信小程序经常有同时需要query参数和body参数的场景 */ -export function postFooAPI2(name: string) { - return http.post('/foo', { name }) -} -/** POST 请求;支持 传递 header 的范例 */ -export function postFooAPI3(name: string) { - return http.post('/foo', { name }, { name }, { 'Content-Type-100': '100' }) -} diff --git a/src/utils/request.ts b/src/utils/request.ts deleted file mode 100644 index 8b16925..0000000 --- a/src/utils/request.ts +++ /dev/null @@ -1,78 +0,0 @@ -import type { CustomRequestOptions } from '@/http/interceptor' - -/** - * 请求方法: 主要是对 uni.request 的封装,去适配 openapi-ts-request 的 request 方法 - * @param options 请求参数 - * @returns 返回 Promise 对象 - */ -function http(options: CustomRequestOptions) { - // 1. 返回 Promise 对象 - return new Promise((resolve, reject) => { - uni.request({ - ...options, - dataType: 'json', - // #ifndef MP-WEIXIN - responseType: 'json', - // #endif - // 响应成功 - success(res) { - // 状态码 2xx,参考 axios 的设计 - if (res.statusCode >= 200 && res.statusCode < 300) { - // 2.1 提取核心数据 res.data - resolve(res.data as T) - } - else if (res.statusCode === 401) { - // 401错误 -> 清理用户信息,跳转到登录页 - // userStore.clearUserInfo() - // uni.navigateTo({ url: '/pages/login/login' }) - reject(res) - } - else { - // 其他错误 -> 根据后端错误信息轻提示 - !options.hideErrorToast - && uni.showToast({ - icon: 'none', - title: (res.data as T & { msg?: string })?.msg || '请求错误', - }) - reject(res) - } - }, - // 响应失败 - fail(err) { - uni.showToast({ - icon: 'none', - title: '网络错误,换个网络试试', - }) - reject(err) - }, - }) - }) -} - -/* - * openapi-ts-request 工具的 request 跨客户端适配方法 - */ -export default function request( - url: string, - options: Omit & { - params?: Record - headers?: Record - }, -) { - const requestOptions = { - url, - ...options, - } - - if (options.params) { - requestOptions.query = requestOptions.params - delete requestOptions.params - } - - if (options.headers) { - requestOptions.header = options.headers - delete requestOptions.headers - } - - return http(requestOptions) -}