diff --git a/api/api-error-plugin.ts b/api/api-error-plugin.ts new file mode 100644 index 0000000..073827a --- /dev/null +++ b/api/api-error-plugin.ts @@ -0,0 +1,22 @@ +import { ZodiosPlugin } from '@zodios/core'; +import { AxiosError } from 'axios'; + +const SKIP_ERROR_HANDLING_URLS = ['/example/skip-error-handling']; + +const errorErrorPlugin: ZodiosPlugin = { + name: 'errorErrorPlugin', + error: async (api, config, err) => { + if (SKIP_ERROR_HANDLING_URLS.includes(config.url)) { + console.log('Skipping error handling for', config.url); + throw err; + } + + if (err instanceof AxiosError) { + console.error('AxiosError', err); + } + + throw err; + }, +}; + +export default errorErrorPlugin; diff --git a/api/api.ts b/api/api.ts new file mode 100644 index 0000000..692493e --- /dev/null +++ b/api/api.ts @@ -0,0 +1,14 @@ +import { Zodios } from '@zodios/core'; +import { ZodiosHooks } from '@zodios/react'; +import apiErrorPlugin from './api-error-plugin'; +import exampleApi from './example'; + +const API_URL = process.env.EXPO_PUBLIC_API_URL || ''; + +// Zodios API client +const apiClient = new Zodios(API_URL, [...exampleApi]); + +apiClient.use(apiErrorPlugin); +const api = new ZodiosHooks('exampleApi', apiClient); + +export { api, apiClient }; diff --git a/api/example/index.ts b/api/example/index.ts new file mode 100644 index 0000000..98a2c91 --- /dev/null +++ b/api/example/index.ts @@ -0,0 +1,47 @@ +import { apiBuilder } from '@zodios/core'; +import { z } from 'zod'; + +// Endpoints for Example API - Example Endpoints. +const exampleApi = apiBuilder({ + method: 'get', + path: '/example', + alias: 'getExample', + description: 'Get example', + response: z.object({ + text: z.string(), + }), + parameters: [ + { + type: 'Query', + name: 'name', + description: 'User name', + schema: z.string().optional(), + }, + ], + errors: [{ status: 'default', schema: z.object({ message: z.string() }) }], +}) + .addEndpoint({ + method: 'post', + path: '/example/:exampleId', + description: 'Add example', + alias: 'addExample', + response: z.object({}), + parameters: [ + { + name: 'exampleId', + type: 'Path', + schema: z.string(), + }, + { + name: 'body', + type: 'Body', + schema: z.object({ + name: z.string(), + }), + }, + ], + errors: [{ status: 'default', schema: z.object({ message: z.string() }) }], + }) + .build(); + +export default exampleApi; diff --git a/app/(authenticated)/index.tsx b/app/(authenticated)/index.tsx index 2bbcd14..117d990 100644 --- a/app/(authenticated)/index.tsx +++ b/app/(authenticated)/index.tsx @@ -1,7 +1,10 @@ +import { useExampleStore } from '@utils/stores/example-store'; import { router } from 'expo-router'; import { Button, Text, View } from 'react-native'; const AuthHomeScreen = () => { + const { value, increment, decrement } = useExampleStore(); + return ( { alignItems: 'center', }}> Authenticated Home + {value} +