From 1cb28f030962d8fffb8c8cd95aef84fb608a62ee Mon Sep 17 00:00:00 2001 From: chenos Date: Thu, 30 Sep 2021 09:45:03 +0800 Subject: [PATCH] fix: client request undefined --- packages/client/src/constate/Client.tsx | 22 +++++++++++++++++++++- packages/client/src/resource.ts | 4 ++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/client/src/constate/Client.tsx b/packages/client/src/constate/Client.tsx index 96d0367c0..2daf5bf9e 100644 --- a/packages/client/src/constate/Client.tsx +++ b/packages/client/src/constate/Client.tsx @@ -3,8 +3,28 @@ import { UseRequestProvider } from 'ahooks'; import { ISchema } from '../schemas'; import { Schema, useField } from '@formily/react'; import { Resource } from '../resource'; +import { ClientSDK } from '../ClientSDK'; +import { extend } from 'umi-request'; -const ClientContext = createContext(null); +export const request = extend({ + prefix: process.env.API_URL, + timeout: 30000, +}); + +request.use(async (ctx, next) => { + const { headers } = ctx.req.options as any; + const token = localStorage.getItem('NOCOBASE_TOKEN'); + if (token) { + headers['Authorization'] = `Bearer ${token}`; + } + await next(); +}); + +const client = new ClientSDK({ + request, +}); + +const ClientContext = createContext(client); export function ClientProvider(props) { const { client } = props; diff --git a/packages/client/src/resource.ts b/packages/client/src/resource.ts index 858a49b38..daae23661 100644 --- a/packages/client/src/resource.ts +++ b/packages/client/src/resource.ts @@ -1,5 +1,5 @@ import { RequestMethod } from 'umi-request'; -import { request } from './schemas'; +import { request as req } from './schemas'; export interface ResourceOptions { resourceName: string; @@ -37,7 +37,7 @@ export class Resource { } else { this.options = options; } - this.request = request || request; + this.request = request || req; } sort(options) {