tsrntemp 1.2.0 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  English | [简体中文](./README_zh-CN.md)
4
4
 
5
- ## :arrow_forward: 用法
5
+ ## :arrow_forward: usage
6
6
  ```sh
7
7
  npx react-native init ProjectName --template tsrntemp
8
8
  ```
@@ -17,7 +17,7 @@ npx react-native init ProjectName --template tsrntemp
17
17
 
18
18
  `The default of this project is TS building, integrated routing, caching, request tool and icon tool`
19
19
 
20
- - 文件目录
20
+ - file directory
21
21
  - src
22
22
  - src/assets`Static resource folder`
23
23
  - src/components`Public component folder`
@@ -27,6 +27,7 @@ npx react-native init ProjectName --template tsrntemp
27
27
  - src/route`Route the page folder`
28
28
  - src/types`Type statement folder`
29
29
  - src/utils`Tool Class Folders`
30
+ - src/requestUtils`Request tool base class`
30
31
 
31
32
  ### react-navigation
32
33
  - [Use reference](https://reactnavigation.org/docs/getting-started)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsrntemp",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "Clean and minimalist React Native template for a quick start with TypeScript.",
5
5
  "scripts": {
6
6
  "test": "exit 0"
@@ -14,21 +14,30 @@
14
14
  "typescript",
15
15
  "jest",
16
16
  "template",
17
- "boilerplate"
17
+ "boilerplate",
18
+ "react-native-typescript-template",
19
+ "react-native-template-typescript"
18
20
  ],
21
+ "engines": {
22
+ "node": ">=16"
23
+ },
19
24
  "author": {
20
25
  "name": "wangzelin",
21
- "url": "https://juejin.cn/user/465848662767367?utm_source=gold_browser_extension"
26
+ "email": "447867010@qq.com"
22
27
  },
23
28
  "contributors": [
24
29
  {
25
30
  "name": "wangzelin",
26
- "url": "https://juejin.cn/user/465848662767367?utm_source=gold_browser_extension"
31
+ "email": "447867010@qq.com"
27
32
  }
28
33
  ],
34
+ "publishConfig": {
35
+ "registry": "https://registry.npmjs.org/",
36
+ "provenance": true
37
+ },
29
38
  "license": "MIT",
30
39
  "bugs": {
31
40
  "url": "https://github.com/wanngzelin/RNTemplate/issues"
32
41
  },
33
42
  "homepage": "https://github.com/wanngzelin/RNTemplate#readme"
34
- }
43
+ }
@@ -0,0 +1,5 @@
1
+ // 请求基基地值
2
+ export const baseUrl = '';
3
+
4
+ // 导出请求地址配置
5
+ //export const apiCOnfig={}
@@ -1,2 +1,3 @@
1
1
  export * from './color';
2
2
  export * from './routesConfig';
3
+ export * from './apiConfig';
@@ -0,0 +1,68 @@
1
+ /**
2
+ * 请求工具基类,支持传入泛型累,可提示返回数据类型
3
+ * 默认响应为ResponseData泛型,如后端返回格式不一致,可修改对应基类
4
+ */
5
+ import {RequestOptionsInit} from 'umi-request';
6
+ import {request} from './request';
7
+ interface Page<T> {
8
+ hasNextPage: boolean;
9
+ hasPrevious: boolean;
10
+ list: T[];
11
+ pageIndex: number;
12
+ pageSize: number;
13
+ total: number;
14
+ }
15
+
16
+ interface ResponseData<T = any> {
17
+ data: T;
18
+ message: string;
19
+ status: number;
20
+ success: boolean;
21
+ timestamp: number;
22
+ }
23
+
24
+ interface HTTPConfig {
25
+ url: string;
26
+ opts?: RequestOptionsInit;
27
+ }
28
+
29
+ type List<T = any[]> = ResponseData<T[]>;
30
+ type ResPage<T = any> = ResponseData<Page<T>>;
31
+
32
+ export class ApiService {
33
+ static async get<T = any>(
34
+ url: string,
35
+ params?: RequestOptionsInit['params'],
36
+ ): Promise<ResponseData<T>> {
37
+ return await request.get<ResponseData<T>>(url, {params});
38
+ }
39
+
40
+ static async post<T = any>(
41
+ url: string,
42
+ data?: RequestOptionsInit['data'],
43
+ ): Promise<ResponseData<T>> {
44
+ return await request.post<ResponseData<T>>(url, {data});
45
+ }
46
+
47
+ static async postQuery<T = any>(
48
+ url: string,
49
+ params?: RequestOptionsInit['params'],
50
+ ): Promise<ResponseData<T>> {
51
+ return await request.post<ResponseData<T>>(url, {params});
52
+ }
53
+
54
+ static async delete<T = any>({
55
+ url,
56
+ opts,
57
+ }: HTTPConfig): Promise<ResponseData<T>> {
58
+ return await request.delete<ResponseData<T>>(url, opts);
59
+ }
60
+
61
+ static async page<T = any>({url, opts}: HTTPConfig): Promise<ResPage<T>> {
62
+ return await request.get<ResPage<T>>(url, opts);
63
+ }
64
+
65
+ static async list<T = any>({url, opts}: HTTPConfig): Promise<List<T>> {
66
+ return await request.get<List<T>>(url, opts);
67
+ }
68
+ }
@@ -0,0 +1 @@
1
+ export * from './ApiService';
@@ -0,0 +1,80 @@
1
+ //请求工具库
2
+ /**
3
+ * 使用可参照https://www.npmjs.com/package/umi-request
4
+ */
5
+ import {extend, ResponseError} from 'umi-request';
6
+ import {Storage} from '@/utils';
7
+ import {baseUrl} from '@/config';
8
+
9
+ const codeMessage: Record<string, string> = {
10
+ '200': '服务器成功返回请求的数据。',
11
+ '201': '新建或修改数据成功。',
12
+ '202': '一个请求已经进入后台排队(异步任务)。',
13
+ '204': '删除数据成功。',
14
+ '400': '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
15
+ '401': '用户没有权限(令牌、用户名、密码错误)。',
16
+ '403': '用户得到授权,但是访问是被禁止的。',
17
+ '404': '发出的请求针对的是不存在的记录,服务器没有进行操作。',
18
+ '406': '请求的格式不可得。',
19
+ '410': '请求的资源被永久删除,且不会再得到的。',
20
+ '422': '当创建一个对象时,发生一个验证错误。',
21
+ '500': '服务器发生错误,请检查服务器。',
22
+ '502': '网关错误。',
23
+ '503': '服务不可用,服务器暂时过载或维护。',
24
+ '504': '网关超时。',
25
+ };
26
+ /**
27
+ * 异常处理程序
28
+ */
29
+ const errorHandler = (error: ResponseError) => {
30
+ const {response} = error;
31
+ console.log(error);
32
+
33
+ if (response && response.status) {
34
+ const {status, url} = response;
35
+ console.log(
36
+ `请求错误 ${status}: ${url},message:${codeMessage[status + '']}`,
37
+ );
38
+ } else if (!response) {
39
+ console.log('您的网络发生异常,无法连接服务器');
40
+ }
41
+ return response;
42
+ };
43
+
44
+ const request = extend({
45
+ prefix: baseUrl,
46
+ timeout: 1000 * 60,
47
+ errorHandler,
48
+ });
49
+
50
+ request.use(
51
+ async (ctx, next) => {
52
+ const {req} = ctx;
53
+ const {options} = req;
54
+ const Authorization = Storage.getString('Authorization');
55
+ ctx.req.options = {
56
+ ...options,
57
+ headers: {
58
+ ...options.headers,
59
+ DeviceType: 'APP',
60
+ Authorization: Authorization ?? '',
61
+ },
62
+ };
63
+ await next();
64
+ },
65
+ {global: true},
66
+ );
67
+
68
+ request.interceptors.response.use(async (res: Response) => {
69
+ const resopnse = await res.clone().json();
70
+ if (res.ok && !resopnse.success) {
71
+ console.log(resopnse?.message || '网络故障');
72
+ // 如果是登录过期,清除缓存,重载app
73
+ if (resopnse?.status === 407) {
74
+ Storage.clearAll();
75
+ }
76
+ }
77
+ return res;
78
+ });
79
+
80
+ export {request};
@@ -1,2 +1,2 @@
1
1
  export * from './storage';
2
- export * from './request';
2
+ export * from '../requestUtils/request';
@@ -1,3 +1,5 @@
1
1
  import {MMKV} from 'react-native-mmkv';
2
2
 
3
- export default new MMKV();
3
+ const Storage = new MMKV();
4
+
5
+ export {Storage};