@snail-js/api 0.1.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.
@@ -0,0 +1,28 @@
1
+ import { RequestBody } from "./request.body";
2
+ import { Api } from "../core/api";
3
+ import { ResponseErrorData, ResponseSuccessData, PaginationData } from "./response.data";
4
+ export interface PipeResult {
5
+ data: RequestBody | undefined;
6
+ headers: Record<string, string>;
7
+ }
8
+ export type RequestPipe = (data: RequestBody | undefined, headers: Record<string, string>) => PipeResult;
9
+ export interface ApiConfig<R = any> {
10
+ name?: string;
11
+ timeout?: number;
12
+ version?: string;
13
+ transform?: (data: R | PaginationData<R>) => R | PaginationData<R>;
14
+ headers?: Record<string, string>;
15
+ params?: Record<string, string>;
16
+ hitSource?: string | Api;
17
+ }
18
+ export interface SendOptions {
19
+ params?: Record<string, string>;
20
+ data?: RequestBody;
21
+ version?: string;
22
+ }
23
+ export interface ApiResponse<T, E> {
24
+ error: ResponseErrorData<E> | null;
25
+ data: ResponseSuccessData<T> | null;
26
+ hitCache: boolean;
27
+ Catch: (error: any) => any;
28
+ }
@@ -0,0 +1,19 @@
1
+ export declare enum CacheType {
2
+ Memory = 0,
3
+ IndexDB = 1,
4
+ LocalStorage = 2
5
+ }
6
+ interface MemoryCacheConfig {
7
+ type: CacheType.Memory;
8
+ }
9
+ interface IndexDBCacheConfig {
10
+ type: CacheType.IndexDB;
11
+ }
12
+ interface LocalStorageCacheConfig {
13
+ type: CacheType.LocalStorage;
14
+ }
15
+ interface CacheCommonConfig {
16
+ ttl?: number;
17
+ }
18
+ export type CacheManagementConfig = CacheCommonConfig & (MemoryCacheConfig | IndexDBCacheConfig | LocalStorageCacheConfig);
19
+ export {};
@@ -0,0 +1,8 @@
1
+ import MemoryCache from "../cache/memoryCache";
2
+ import LocalStorageCache from "../cache/localstorageCache";
3
+ import IndexDBCache from "../cache/indexDBCache";
4
+ export interface CacheData<T = any> {
5
+ data: T;
6
+ exp: number;
7
+ }
8
+ export type CacheStorage = MemoryCache | LocalStorageCache | IndexDBCache;
@@ -0,0 +1,5 @@
1
+ export declare enum ContentType {
2
+ JSON = "application/json;charset=UTF-8",
3
+ FORM_URLENCODED = "application/x-www-form-urlencoded;charset=UTF-8",
4
+ FORM_DATA = "multipart/form-data;charset=UTF-8"
5
+ }
@@ -0,0 +1,8 @@
1
+ export * from "./request.body";
2
+ export * from "./request.method";
3
+ export * from "./snail.config";
4
+ export * from "./api.config";
5
+ export * from "./versioning.config";
6
+ export * from "./cache.management.config";
7
+ export * from "./cache.type";
8
+ export * from "./response.data";
@@ -0,0 +1,3 @@
1
+ type JsonBody = Record<string, any>;
2
+ export type RequestBody = JsonBody | string | FormData | Blob | ArrayBuffer | URLSearchParams | ReadableStream;
3
+ export {};
@@ -0,0 +1,9 @@
1
+ export declare enum RequestMethod {
2
+ GET = "GET",
3
+ HEAD = "HEAD",
4
+ POST = "POST",
5
+ PUT = "PUT",
6
+ DELETE = "DELETE",
7
+ PATCH = "PATCH",
8
+ OPTIONS = "OPTIONS"
9
+ }
@@ -0,0 +1,17 @@
1
+ export interface ResponseErrorData<E = any> {
2
+ code: number;
3
+ message: string;
4
+ data: E;
5
+ }
6
+ export interface PaginationData<T> {
7
+ total: number;
8
+ page: number;
9
+ size: number;
10
+ record: T[];
11
+ }
12
+ export interface ResponseSuccessData<T = any> {
13
+ code: 0;
14
+ message: string;
15
+ data: T | PaginationData<T>;
16
+ }
17
+ export type ResponseData<T, E> = ResponseSuccessData<T> | ResponseErrorData<E>;
@@ -0,0 +1,21 @@
1
+ import { AxiosInterceptorOptions, AxiosResponse, InternalAxiosRequestConfig } from "axios";
2
+ import { CacheManagementConfig } from "./cache.management.config";
3
+ import { VersioningConfig } from "./versioning.config";
4
+ export type AxiosRequestInterceptor<T = any> = {
5
+ onFulfilled?: ((value: InternalAxiosRequestConfig<T>) => InternalAxiosRequestConfig<T> | Promise<InternalAxiosRequestConfig<T>>) | null;
6
+ onRejected?: ((error: any) => any) | null;
7
+ options?: AxiosInterceptorOptions;
8
+ };
9
+ export type AxiosResponseInterceptor<T = any> = {
10
+ onFulfilled?: ((response: AxiosResponse<T>) => AxiosResponse<T> | Promise<AxiosResponse<T>>) | null;
11
+ onRejected?: ((error: any) => any) | null;
12
+ };
13
+ export interface SnailConfig {
14
+ baseURL?: string;
15
+ Versioning?: VersioningConfig;
16
+ CacheManage?: CacheManagementConfig;
17
+ enableLog?: boolean;
18
+ timeout?: number;
19
+ requestInterceptors?: AxiosRequestInterceptor;
20
+ responseInterceptors?: AxiosResponseInterceptor;
21
+ }
@@ -0,0 +1,30 @@
1
+ export declare enum VersioningType {
2
+ Uri = 0,
3
+ Header = 1,
4
+ Query = 2,
5
+ Custom = 3
6
+ }
7
+ interface VersioningCommonConfig {
8
+ defaultVersion: string;
9
+ }
10
+ export interface VersioningUriConfig extends VersioningCommonConfig {
11
+ type: VersioningType.Uri;
12
+ prefix?: string;
13
+ }
14
+ export interface VersioningHeaderConfig extends VersioningCommonConfig {
15
+ type: VersioningType.Header;
16
+ header: string;
17
+ }
18
+ export interface VersioningQueryConfig extends VersioningCommonConfig {
19
+ type: VersioningType.Query;
20
+ key?: string;
21
+ }
22
+ export interface VersioningCustomConfig extends VersioningCommonConfig {
23
+ type: VersioningType.Custom;
24
+ extractor: (requestOptions: unknown) => {
25
+ url: string;
26
+ headers: Record<string, any>;
27
+ };
28
+ }
29
+ export type VersioningConfig = VersioningUriConfig | VersioningHeaderConfig | VersioningQueryConfig | VersioningCustomConfig;
30
+ export {};
@@ -0,0 +1,11 @@
1
+ export declare const apiKey: <T extends {
2
+ url: string;
3
+ method: string;
4
+ data?: any;
5
+ version?: string;
6
+ params?: Record<string, any>;
7
+ headers?: Record<string, any>;
8
+ }>(apiInstance: T) => string;
9
+ export declare function recordToString(record: Record<string, any>): string;
10
+ export declare function getResponseDataFromCache(cacheKey: string): string;
11
+ export declare function deepCopy<T>(obj: T): T;
@@ -0,0 +1 @@
1
+ export * from './function';
@@ -0,0 +1,7 @@
1
+ import { VersioningConfig } from "../typings";
2
+ interface VersioningResult {
3
+ url?: string;
4
+ headers?: Record<string, any>;
5
+ }
6
+ export declare function applyVersioning(version: string, versioning: VersioningConfig): VersioningResult;
7
+ export {};
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@snail-js/api",
3
+ "version": "0.1.0",
4
+ "description": "",
5
+ "main": "index.ts",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./dist/snail-api.js",
9
+ "require": "./dist/snail-api.umd.cjs",
10
+ "types": "./dist/index.d.ts"
11
+ }
12
+ },
13
+ "scripts": {
14
+ "clean:dist": "rimraf ./dist",
15
+ "build": "pnpm run clean:dist && vite build",
16
+ "test": "echo \"Error: no test specified\" && exit 1"
17
+ },
18
+ "keywords": [],
19
+ "author": "",
20
+ "license": "MIT",
21
+ "peerDependencies": {
22
+ "axios": "^1.7.9"
23
+ },
24
+ "files": [
25
+ "README.md",
26
+ "dist"
27
+ ]
28
+ }