simpleloan_api 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './src'
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "simpleloan_api",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "keywords": ["simpleloan"],
9
+ "author": "lazyboneskid",
10
+ "license": "ISC",
11
+ "description": "simpleloan api methods with types",
12
+ "dependencies": {
13
+ "vue": "^3.2.13"
14
+ }
15
+ }
@@ -0,0 +1,84 @@
1
+ import { Ref, ref } from "vue";
2
+ import { ApiResponse, PostParams, SearchParams } from "./types";
3
+ import { getToken, mode } from "../settings";
4
+
5
+ async function getContent(response: Response) {
6
+ const contentType = response.headers.get("Content-Type");
7
+ if (!contentType) return null;
8
+
9
+ if (contentType.includes("application/json")) {
10
+ return response.json();
11
+ }
12
+
13
+ if (contentType.includes("text")) {
14
+ return response.text();
15
+ }
16
+
17
+ if (
18
+ contentType.includes("application/octet-stream") ||
19
+ contentType.includes("blob")
20
+ ) {
21
+ return response.blob();
22
+ }
23
+ }
24
+
25
+ function getHeaders() {
26
+ return {
27
+ Accept: "application/json, text/plain, */*",
28
+ "Content-Type": "application/json; text/html; image/png",
29
+ };
30
+ }
31
+
32
+ function getApiUrl() {
33
+ return mode.value === "production"
34
+ ? "portal.simpleloan.ru/api/"
35
+ : "slb.medv.ru/api/";
36
+ }
37
+
38
+ function getBody(params: { body?: any; search?: SearchParams }) {
39
+ return JSON.stringify({
40
+ _data: params.body || undefined,
41
+ _searchParams: params.search || undefined,
42
+ _token: getToken(),
43
+ });
44
+ }
45
+
46
+ type FetchReturn<T, Body> = {
47
+ response: Ref<ApiResponse<T> | null>;
48
+ pending: Ref<boolean>;
49
+ post(params: PostParams<Body>): Promise<ApiResponse<T> | null>;
50
+ };
51
+
52
+ export function useFetch<T, Body>(url: string): FetchReturn<T, Body>;
53
+
54
+ export function useFetch<T, Body>(url: string) {
55
+ const response = ref<ApiResponse<T> | null>(
56
+ null
57
+ ) as Ref<ApiResponse<T> | null>;
58
+ const pending = ref<boolean>(false);
59
+ const fullUrl = getApiUrl() + url;
60
+
61
+ async function post(params?: PostParams<Body>) {
62
+ pending.value = true;
63
+
64
+ const body = params?.body;
65
+ const search = params?.search;
66
+
67
+ try {
68
+ const fetchResponse = await fetch(fullUrl, {
69
+ method: "POST",
70
+ body: getBody({ body, search }),
71
+ headers: getHeaders(),
72
+ });
73
+
74
+ const content = await getContent(fetchResponse);
75
+ response.value = content;
76
+ } catch (error) {
77
+ response.value = null;
78
+ }
79
+
80
+ return response.value;
81
+ }
82
+
83
+ return { post, response, pending };
84
+ }
@@ -0,0 +1,20 @@
1
+ export type ApiResponse<T> = {
2
+ _code: number;
3
+ _message?: string;
4
+ _detail?: { [param: string]: string[] };
5
+ _data_total_size?: number;
6
+ _data?: T;
7
+ };
8
+
9
+ export type SearchParams = {
10
+ _filter?: Record<string, any>;
11
+ _page_size?: number;
12
+ _fields?: string[];
13
+ _start_index?: number;
14
+ _sort?: string[];
15
+ };
16
+
17
+ export type PostParams<Body> = {
18
+ body: Body;
19
+ search?: SearchParams;
20
+ };
package/src/index.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { ref } from "vue";
2
+
3
+ type Mode = "development" | "production";
4
+
5
+ let token: string | null = null;
6
+
7
+ const mode = ref<Mode>("development");
8
+
9
+ export function useApiSettings() {
10
+ function setMode(newMode: Mode) {
11
+ mode.value = newMode;
12
+ }
13
+
14
+ function getMode() {
15
+ return mode.value
16
+ }
17
+
18
+ function setToken(newToken: string) {
19
+ token = newToken;
20
+ }
21
+
22
+ function removeToken() {
23
+ token = null;
24
+ }
25
+
26
+ function getToken() {
27
+ return token;
28
+ }
29
+
30
+ return { setMode, getMode, setToken, removeToken, getToken };
31
+ }
32
+
@@ -0,0 +1,7 @@
1
+ import { useFetch } from "../../fetch";
2
+ import { General } from "./types";
3
+
4
+ export const useGetGeneral = () =>
5
+ useFetch<General, { id: number }>("general/get/");
6
+
7
+
@@ -0,0 +1,5 @@
1
+ export type General = {
2
+ id: number;
3
+ user_id: number;
4
+ salespoint_id: number;
5
+ }
@@ -0,0 +1 @@
1
+ export * from './general'
@@ -0,0 +1,31 @@
1
+ import { computed, ref } from "vue";
2
+
3
+ type Mode = "development" | "production";
4
+
5
+ let token: string | null = null;
6
+
7
+ export const mode = ref<Mode>("development");
8
+
9
+ export function setMode(newMode: Mode) {
10
+ mode.value = newMode;
11
+ }
12
+
13
+
14
+ export function getMode() {
15
+ return mode.value
16
+ }
17
+
18
+
19
+ export function setToken(newToken: string) {
20
+ token = newToken;
21
+ }
22
+
23
+
24
+ export function removeToken() {
25
+ token = null;
26
+ }
27
+
28
+
29
+ export function getToken() {
30
+ return token;
31
+ }