@snail-js/api 0.1.27 → 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.
Files changed (313) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +125 -605
  3. package/dist/adapter/plain.d.ts +12 -0
  4. package/dist/adapter/plain.d.ts.map +1 -0
  5. package/dist/adapter/react.d.ts +14 -0
  6. package/dist/adapter/react.d.ts.map +1 -0
  7. package/dist/adapter/registry.d.ts +16 -0
  8. package/dist/adapter/registry.d.ts.map +1 -0
  9. package/dist/adapter/vue.d.ts +15 -0
  10. package/dist/adapter/vue.d.ts.map +1 -0
  11. package/dist/chunks/custom-UchOSg_s.js +466 -0
  12. package/dist/chunks/custom-UchOSg_s.js.map +1 -0
  13. package/dist/chunks/download-lBC7oOTT.js +160 -0
  14. package/dist/chunks/download-lBC7oOTT.js.map +1 -0
  15. package/dist/chunks/object-Ct0w-h4i.js +74 -0
  16. package/dist/chunks/object-Ct0w-h4i.js.map +1 -0
  17. package/dist/chunks/plugin-D3RDffDG.js +742 -0
  18. package/dist/chunks/plugin-D3RDffDG.js.map +1 -0
  19. package/dist/chunks/react-C1em-gxy.js +62 -0
  20. package/dist/chunks/react-C1em-gxy.js.map +1 -0
  21. package/dist/chunks/request-qeRhihEF.js +40 -0
  22. package/dist/chunks/request-qeRhihEF.js.map +1 -0
  23. package/dist/chunks/response-D-ubkAFl.js +127 -0
  24. package/dist/chunks/response-D-ubkAFl.js.map +1 -0
  25. package/dist/chunks/use-download-FAjtdJ58.js +1816 -0
  26. package/dist/chunks/use-download-FAjtdJ58.js.map +1 -0
  27. package/dist/chunks/vue-Dd9EhkQ3.js +34 -0
  28. package/dist/chunks/vue-Dd9EhkQ3.js.map +1 -0
  29. package/dist/core/args.d.ts +54 -0
  30. package/dist/core/args.d.ts.map +1 -0
  31. package/dist/core/connection.d.ts +15 -0
  32. package/dist/core/connection.d.ts.map +1 -0
  33. package/dist/core/context.d.ts +124 -0
  34. package/dist/core/context.d.ts.map +1 -0
  35. package/dist/core/http-stream.d.ts +28 -0
  36. package/dist/core/http-stream.d.ts.map +1 -0
  37. package/dist/core/logger.d.ts +22 -0
  38. package/dist/core/logger.d.ts.map +1 -0
  39. package/dist/core/metadata.d.ts +92 -0
  40. package/dist/core/metadata.d.ts.map +1 -0
  41. package/dist/core/metadata.keys.d.ts +41 -0
  42. package/dist/core/metadata.keys.d.ts.map +1 -0
  43. package/dist/core/method.d.ts +177 -0
  44. package/dist/core/method.d.ts.map +1 -0
  45. package/dist/core/plugin-manager.d.ts +106 -0
  46. package/dist/core/plugin-manager.d.ts.map +1 -0
  47. package/dist/core/plugin.d.ts +113 -0
  48. package/dist/core/plugin.d.ts.map +1 -0
  49. package/dist/core/reconnect.d.ts +22 -0
  50. package/dist/core/reconnect.d.ts.map +1 -0
  51. package/dist/core/resolve-stream.d.ts +45 -0
  52. package/dist/core/resolve-stream.d.ts.map +1 -0
  53. package/dist/core/resolve.d.ts +50 -0
  54. package/dist/core/resolve.d.ts.map +1 -0
  55. package/dist/core/response.d.ts +60 -0
  56. package/dist/core/response.d.ts.map +1 -0
  57. package/dist/core/server.d.ts +132 -0
  58. package/dist/core/server.d.ts.map +1 -0
  59. package/dist/core/sse.d.ts +27 -0
  60. package/dist/core/sse.d.ts.map +1 -0
  61. package/dist/core/state-bag.d.ts +36 -0
  62. package/dist/core/state-bag.d.ts.map +1 -0
  63. package/dist/core/websocket.d.ts +29 -0
  64. package/dist/core/websocket.d.ts.map +1 -0
  65. package/dist/decorators/api.d.ts +24 -4
  66. package/dist/decorators/api.d.ts.map +1 -0
  67. package/dist/decorators/args.d.ts +77 -14
  68. package/dist/decorators/args.d.ts.map +1 -0
  69. package/dist/decorators/custom.d.ts +81 -0
  70. package/dist/decorators/custom.d.ts.map +1 -0
  71. package/dist/decorators/header.d.ts +22 -0
  72. package/dist/decorators/header.d.ts.map +1 -0
  73. package/dist/decorators/index.d.ts +17 -0
  74. package/dist/decorators/index.d.ts.map +1 -0
  75. package/dist/decorators/methods.d.ts +33 -0
  76. package/dist/decorators/methods.d.ts.map +1 -0
  77. package/dist/decorators/progress.d.ts +24 -6
  78. package/dist/decorators/progress.d.ts.map +1 -0
  79. package/dist/decorators/server.d.ts +28 -7
  80. package/dist/decorators/server.d.ts.map +1 -0
  81. package/dist/decorators/stream.d.ts +113 -0
  82. package/dist/decorators/stream.d.ts.map +1 -0
  83. package/dist/default/options.d.ts +19 -0
  84. package/dist/default/options.d.ts.map +1 -0
  85. package/dist/error/base.d.ts +20 -0
  86. package/dist/error/base.d.ts.map +1 -0
  87. package/dist/error/decorator.d.ts +14 -0
  88. package/dist/error/decorator.d.ts.map +1 -0
  89. package/dist/error/hook.d.ts +14 -0
  90. package/dist/error/hook.d.ts.map +1 -0
  91. package/dist/error/index.d.ts +8 -0
  92. package/dist/error/index.d.ts.map +1 -0
  93. package/dist/error/options.d.ts +12 -0
  94. package/dist/error/options.d.ts.map +1 -0
  95. package/dist/error/plugin.d.ts +11 -0
  96. package/dist/error/plugin.d.ts.map +1 -0
  97. package/dist/error/request.d.ts +29 -0
  98. package/dist/error/request.d.ts.map +1 -0
  99. package/dist/error/response.d.ts +36 -0
  100. package/dist/error/response.d.ts.map +1 -0
  101. package/dist/index.d.ts +56 -11
  102. package/dist/index.d.ts.map +1 -0
  103. package/dist/index.js +2034 -0
  104. package/dist/index.js.map +1 -0
  105. package/dist/locale/en.d.ts +5 -0
  106. package/dist/locale/en.d.ts.map +1 -0
  107. package/dist/locale/index.d.ts +57 -0
  108. package/dist/locale/index.d.ts.map +1 -0
  109. package/dist/locale/types.d.ts +11 -0
  110. package/dist/locale/types.d.ts.map +1 -0
  111. package/dist/locale/zh.d.ts +5 -0
  112. package/dist/locale/zh.d.ts.map +1 -0
  113. package/dist/plugins/cache/adapters/indexeddb.d.ts +52 -0
  114. package/dist/plugins/cache/adapters/indexeddb.d.ts.map +1 -0
  115. package/dist/plugins/cache/adapters/memory.d.ts +60 -0
  116. package/dist/plugins/cache/adapters/memory.d.ts.map +1 -0
  117. package/dist/plugins/cache/adapters/web-storage.d.ts +43 -0
  118. package/dist/plugins/cache/adapters/web-storage.d.ts.map +1 -0
  119. package/dist/plugins/cache/decorators.d.ts +62 -0
  120. package/dist/plugins/cache/decorators.d.ts.map +1 -0
  121. package/dist/plugins/cache/index.d.ts +41 -0
  122. package/dist/plugins/cache/index.d.ts.map +1 -0
  123. package/dist/plugins/cache/key.d.ts +48 -0
  124. package/dist/plugins/cache/key.d.ts.map +1 -0
  125. package/dist/plugins/cache/manager.d.ts +124 -0
  126. package/dist/plugins/cache/manager.d.ts.map +1 -0
  127. package/dist/plugins/cache/plugin.d.ts +64 -0
  128. package/dist/plugins/cache/plugin.d.ts.map +1 -0
  129. package/dist/plugins/cache/type.d.ts +116 -0
  130. package/dist/plugins/cache/type.d.ts.map +1 -0
  131. package/dist/plugins/index.d.ts +75 -0
  132. package/dist/plugins/index.d.ts.map +1 -0
  133. package/dist/plugins/index.js +2265 -0
  134. package/dist/plugins/index.js.map +1 -0
  135. package/dist/plugins/interceptor/decorators.d.ts +44 -0
  136. package/dist/plugins/interceptor/decorators.d.ts.map +1 -0
  137. package/dist/plugins/interceptor/index.d.ts +31 -0
  138. package/dist/plugins/interceptor/index.d.ts.map +1 -0
  139. package/dist/plugins/interceptor/manager.d.ts +27 -0
  140. package/dist/plugins/interceptor/manager.d.ts.map +1 -0
  141. package/dist/plugins/interceptor/plugin.d.ts +65 -0
  142. package/dist/plugins/interceptor/plugin.d.ts.map +1 -0
  143. package/dist/plugins/interceptor/type.d.ts +45 -0
  144. package/dist/plugins/interceptor/type.d.ts.map +1 -0
  145. package/dist/plugins/pool/index.d.ts +26 -0
  146. package/dist/plugins/pool/index.d.ts.map +1 -0
  147. package/dist/plugins/pool/plugin.d.ts +83 -0
  148. package/dist/plugins/pool/plugin.d.ts.map +1 -0
  149. package/dist/plugins/pool/scheduler.d.ts +153 -0
  150. package/dist/plugins/pool/scheduler.d.ts.map +1 -0
  151. package/dist/plugins/pool/type.d.ts +38 -0
  152. package/dist/plugins/pool/type.d.ts.map +1 -0
  153. package/dist/plugins/react/index.d.ts +20 -0
  154. package/dist/plugins/react/index.d.ts.map +1 -0
  155. package/dist/plugins/react/index.js +145 -0
  156. package/dist/plugins/react/index.js.map +1 -0
  157. package/dist/plugins/react/plugin.d.ts +33 -0
  158. package/dist/plugins/react/plugin.d.ts.map +1 -0
  159. package/dist/plugins/react/type.d.ts +32 -0
  160. package/dist/plugins/react/type.d.ts.map +1 -0
  161. package/dist/plugins/transform/decorators.d.ts +54 -0
  162. package/dist/plugins/transform/decorators.d.ts.map +1 -0
  163. package/dist/plugins/transform/hydrate.d.ts +47 -0
  164. package/dist/plugins/transform/hydrate.d.ts.map +1 -0
  165. package/dist/plugins/transform/index.d.ts +41 -0
  166. package/dist/plugins/transform/index.d.ts.map +1 -0
  167. package/dist/plugins/transform/plugin.d.ts +20 -0
  168. package/dist/plugins/transform/plugin.d.ts.map +1 -0
  169. package/dist/plugins/transform/type.d.ts +67 -0
  170. package/dist/plugins/transform/type.d.ts.map +1 -0
  171. package/dist/plugins/validate/decorators.d.ts +24 -0
  172. package/dist/plugins/validate/decorators.d.ts.map +1 -0
  173. package/dist/plugins/validate/index.d.ts +40 -0
  174. package/dist/plugins/validate/index.d.ts.map +1 -0
  175. package/dist/plugins/validate/plugin.d.ts +13 -0
  176. package/dist/plugins/validate/plugin.d.ts.map +1 -0
  177. package/dist/plugins/validate/type.d.ts +54 -0
  178. package/dist/plugins/validate/type.d.ts.map +1 -0
  179. package/dist/plugins/version/decorators.d.ts +22 -0
  180. package/dist/plugins/version/decorators.d.ts.map +1 -0
  181. package/dist/plugins/version/index.d.ts +23 -0
  182. package/dist/plugins/version/index.d.ts.map +1 -0
  183. package/dist/plugins/version/plugin.d.ts +13 -0
  184. package/dist/plugins/version/plugin.d.ts.map +1 -0
  185. package/dist/plugins/version/type.d.ts +47 -0
  186. package/dist/plugins/version/type.d.ts.map +1 -0
  187. package/dist/plugins/vue/index.d.ts +21 -0
  188. package/dist/plugins/vue/index.d.ts.map +1 -0
  189. package/dist/plugins/vue/index.js +114 -0
  190. package/dist/plugins/vue/index.js.map +1 -0
  191. package/dist/plugins/vue/plugin.d.ts +15 -0
  192. package/dist/plugins/vue/plugin.d.ts.map +1 -0
  193. package/dist/plugins/vue/type.d.ts +34 -0
  194. package/dist/plugins/vue/type.d.ts.map +1 -0
  195. package/dist/strategies/index.d.ts +2 -0
  196. package/dist/strategies/index.d.ts.map +1 -0
  197. package/dist/strategies/index.js +29 -0
  198. package/dist/strategies/index.js.map +1 -0
  199. package/dist/strategies/plain.d.ts +2 -0
  200. package/dist/strategies/plain.d.ts.map +1 -0
  201. package/dist/strategies/plain.js +23 -0
  202. package/dist/strategies/plain.js.map +1 -0
  203. package/dist/strategies/react.d.ts +2 -0
  204. package/dist/strategies/react.d.ts.map +1 -0
  205. package/dist/strategies/react.js +30 -0
  206. package/dist/strategies/react.js.map +1 -0
  207. package/dist/strategies/shared/adapter.d.ts +23 -0
  208. package/dist/strategies/shared/adapter.d.ts.map +1 -0
  209. package/dist/strategies/shared/dom.d.ts +53 -0
  210. package/dist/strategies/shared/dom.d.ts.map +1 -0
  211. package/dist/strategies/shared/error.d.ts +30 -0
  212. package/dist/strategies/shared/error.d.ts.map +1 -0
  213. package/dist/strategies/shared/method.d.ts +51 -0
  214. package/dist/strategies/shared/method.d.ts.map +1 -0
  215. package/dist/strategies/shared/public.d.ts +33 -0
  216. package/dist/strategies/shared/public.d.ts.map +1 -0
  217. package/dist/strategies/shared/queue.d.ts +30 -0
  218. package/dist/strategies/shared/queue.d.ts.map +1 -0
  219. package/dist/strategies/shared/retry.d.ts +51 -0
  220. package/dist/strategies/shared/retry.d.ts.map +1 -0
  221. package/dist/strategies/shared/state.d.ts +117 -0
  222. package/dist/strategies/shared/state.d.ts.map +1 -0
  223. package/dist/strategies/shared/timing.d.ts +53 -0
  224. package/dist/strategies/shared/timing.d.ts.map +1 -0
  225. package/dist/strategies/shared/watcher.d.ts +37 -0
  226. package/dist/strategies/shared/watcher.d.ts.map +1 -0
  227. package/dist/strategies/use-auto-request.d.ts +77 -0
  228. package/dist/strategies/use-auto-request.d.ts.map +1 -0
  229. package/dist/strategies/use-download.d.ts +103 -0
  230. package/dist/strategies/use-download.d.ts.map +1 -0
  231. package/dist/strategies/use-fetcher.d.ts +52 -0
  232. package/dist/strategies/use-fetcher.d.ts.map +1 -0
  233. package/dist/strategies/use-pagination.d.ts +101 -0
  234. package/dist/strategies/use-pagination.d.ts.map +1 -0
  235. package/dist/strategies/use-request.d.ts +50 -0
  236. package/dist/strategies/use-request.d.ts.map +1 -0
  237. package/dist/strategies/use-retriable-request.d.ts +46 -0
  238. package/dist/strategies/use-retriable-request.d.ts.map +1 -0
  239. package/dist/strategies/use-sse.d.ts +103 -0
  240. package/dist/strategies/use-sse.d.ts.map +1 -0
  241. package/dist/strategies/use-token-auth.d.ts +88 -0
  242. package/dist/strategies/use-token-auth.d.ts.map +1 -0
  243. package/dist/strategies/use-uploader.d.ts +88 -0
  244. package/dist/strategies/use-uploader.d.ts.map +1 -0
  245. package/dist/strategies/use-watcher.d.ts +69 -0
  246. package/dist/strategies/use-watcher.d.ts.map +1 -0
  247. package/dist/typings/adapter.d.ts +61 -0
  248. package/dist/typings/adapter.d.ts.map +1 -0
  249. package/dist/typings/api.d.ts +122 -0
  250. package/dist/typings/api.d.ts.map +1 -0
  251. package/dist/typings/args.d.ts +51 -0
  252. package/dist/typings/args.d.ts.map +1 -0
  253. package/dist/typings/index.d.ts +8 -12
  254. package/dist/typings/index.d.ts.map +1 -0
  255. package/dist/typings/meta.d.ts +44 -0
  256. package/dist/typings/meta.d.ts.map +1 -0
  257. package/dist/typings/plugin.d.ts +114 -0
  258. package/dist/typings/plugin.d.ts.map +1 -0
  259. package/dist/typings/response.d.ts +118 -0
  260. package/dist/typings/response.d.ts.map +1 -0
  261. package/dist/typings/server.d.ts +83 -0
  262. package/dist/typings/server.d.ts.map +1 -0
  263. package/dist/typings/stream.d.ts +150 -0
  264. package/dist/typings/stream.d.ts.map +1 -0
  265. package/dist/utils/download.d.ts +97 -0
  266. package/dist/utils/download.d.ts.map +1 -0
  267. package/dist/utils/emitter.d.ts +36 -0
  268. package/dist/utils/emitter.d.ts.map +1 -0
  269. package/dist/utils/index.d.ts +6 -1
  270. package/dist/utils/index.d.ts.map +1 -0
  271. package/dist/utils/is.d.ts +22 -0
  272. package/dist/utils/is.d.ts.map +1 -0
  273. package/dist/utils/object.d.ts +44 -0
  274. package/dist/utils/object.d.ts.map +1 -0
  275. package/dist/utils/url.d.ts +44 -0
  276. package/dist/utils/url.d.ts.map +1 -0
  277. package/package.json +90 -27
  278. package/README_EN.md +0 -607
  279. package/dist/cache/index.d.ts +0 -9
  280. package/dist/cache/indexDBCache.d.ts +0 -13
  281. package/dist/cache/localstorageCache.d.ts +0 -10
  282. package/dist/cache/memoryCache.d.ts +0 -11
  283. package/dist/core/index.d.ts +0 -4
  284. package/dist/core/snailApi.d.ts +0 -22
  285. package/dist/core/snailMethod.d.ts +0 -49
  286. package/dist/core/snailServer.d.ts +0 -35
  287. package/dist/core/snailSse.d.ts +0 -20
  288. package/dist/decorators/cache.d.ts +0 -15
  289. package/dist/decorators/method.d.ts +0 -38
  290. package/dist/decorators/sse.d.ts +0 -30
  291. package/dist/decorators/strategy.d.ts +0 -10
  292. package/dist/decorators/versioning.d.ts +0 -16
  293. package/dist/eventEmmit/index.d.ts +0 -1
  294. package/dist/eventEmmit/snail.event.d.ts +0 -10
  295. package/dist/snail-api.js +0 -4868
  296. package/dist/snail-api.umd.cjs +0 -4872
  297. package/dist/strategies/jwtStrategy.d.ts +0 -4
  298. package/dist/typings/api.option.d.ts +0 -10
  299. package/dist/typings/apiProxy.d.ts +0 -8
  300. package/dist/typings/cache.management.option.d.ts +0 -25
  301. package/dist/typings/cache.type.d.ts +0 -23
  302. package/dist/typings/content.type.d.ts +0 -5
  303. package/dist/typings/request.body.d.ts +0 -3
  304. package/dist/typings/response.data.d.ts +0 -13
  305. package/dist/typings/snail.event.d.ts +0 -6
  306. package/dist/typings/snail.method.d.ts +0 -14
  307. package/dist/typings/snail.option.d.ts +0 -16
  308. package/dist/typings/sse.d.ts +0 -15
  309. package/dist/typings/strategy.d.ts +0 -5
  310. package/dist/typings/versioning.option.d.ts +0 -30
  311. package/dist/utils/function.d.ts +0 -31
  312. package/dist/versioning/index.d.ts +0 -1
  313. package/dist/versioning/versioning.d.ts +0 -12
package/README_EN.md DELETED
@@ -1,607 +0,0 @@
1
- <p>
2
- <img src="https://img.shields.io/badge/TypeScript-1e80ff"></img>
3
- <img src="https://img.shields.io/npm/v/axios?label=axios&labelColor=1e80ff&color=67C23A"></img>
4
- <img src="https://img.shields.io/npm/v/reflect-metadata?label=reflect-metadata&labelColor=1e80ff&color=67C23A"></img>
5
- </p>
6
-
7
- <a href='./README.md'>中文文档</a>|English Document
8
-
9
- ## Project Introduction
10
-
11
- - Secondary encapsulation based on Axios
12
- - Use `reflect-metadata` to create and process metadata
13
- - Provide decorators to define request methods, supporting all HTTP methods and SSE
14
-
15
- ## Installation
16
-
17
- `npm install @snail-js/api`
18
-
19
- ## Usage
20
-
21
- 1. Enable TypeScript decorator configuration:
22
-
23
- ```json
24
- // tsconfig.json
25
- {
26
- "module": "ESNext",
27
- // Module resolution strategy
28
- "moduleResolution": "node",
29
- "baseUrl": ".",
30
- // Target must be higher than ES6
31
- "target": "ESNext",
32
- // lib should include ES versions above ES6
33
- "lib": ["ESNext", "DOM"],
34
- // Include reflect-metadata types
35
- "types": ["reflect-metadata"],
36
- "emitDecoratorMetadata": true,
37
- "experimentalDecorators": true,
38
-
39
- "skipLibCheck": true,
40
- "strictNullChecks": false
41
- }
42
- ```
43
-
44
- 2. Create Snail backend configuration instance:
45
-
46
- ```ts
47
- // service.ts
48
- import { SnailServer, Server } from "@snail-js/api";
49
-
50
- @Server({
51
- baseURL: "/api",
52
- timeout: 5000,
53
- })
54
- class BackEnd extends SnailServer {}
55
-
56
- export const Service = new BackEnd();
57
- ```
58
-
59
- 3. Create API instance:
60
-
61
- ```ts
62
- // user.ts
63
- import { Api, Get, Post, Query, Data, SnailApi } from "@snail-js/api";
64
- import { Service } from "./service";
65
-
66
- @Api("user")
67
- class UserApi extends SnailApi {
68
- @Get()
69
- get(@Query("id") id: string) {}
70
-
71
- @Post()
72
- create(@Data() user: User) {}
73
- }
74
- // Create and export API
75
- export const userApi = Service.createApi(UserApi);
76
- ```
77
-
78
- 4. Send requests:
79
-
80
- ```ts
81
- import { userApi } from "./user";
82
-
83
- const { send:getUser, onSuccess, onError, onHitCache } = await userApi.get();
84
-
85
- const data = await getUser("1");
86
- ```
87
-
88
- ## `SnailMethod` Instance
89
-
90
- - When calling `Service.createApi(ApiInstance)`, creates a proxy for methods decorated with `RequestMethod` (e.g. @Get, @Post)
91
- - Returns a function containing request parameters, which when called returns a `SnailMethod` instance
92
-
93
- ### `SnailMethod` Methods
94
-
95
- - `send`: Send request
96
- _Async function that executes the current request_
97
- - `onSuccess`: Request success callback
98
- _Register success event handler_
99
- - `onError`: Request failure callback
100
- _Register error event handler_
101
- - `onHitCache`: Cache hit callback
102
- _Register cache hit event handler_
103
- - `onFinish`: Request completion callback
104
- _Register completion event handler (fires on both success/error)_
105
- - `registerStrategies` Register Strategies
106
- _Register Strategies of this method instance_
107
-
108
- ### `SnailMethod` Properties
109
- - `response`: AxiosResponse - Raw response object
110
- - `request`: AxiosRequestConfig - Final processed request after applying Versioning and Strategies
111
- - `version`: string - Effective request version (undefined if Versioning disabled)
112
- - `name`: string - Full method identifier in `ServerName.ApiName.MethodName` format
113
- - `error`: Error | null - Error object if request failed, null otherwise
114
-
115
- ### Server Configuration
116
-
117
- <table>
118
- <tr>
119
- <th>Option</th>
120
- <th>Type</th>
121
- <th>Required</th>
122
- <th>Default Value</th>
123
- <th>Description</th>
124
- </tr>
125
- <tr>
126
- <td>name</td>
127
- <td>string</td>
128
- <td>No</td>
129
- <td>Class name inheriting SnailServer</td>
130
- <td>Unique server identifier</td>
131
- </tr>
132
- <tr>
133
- <td>baseUrl</td>
134
- <td>string</td>
135
- <td>No</td>
136
- <td>'\'</td>
137
- <td>API prefix (same as axios baseURL)</td>
138
- </tr>
139
- <tr>
140
- <td>Versioning</td>
141
- <td><a href="#versioningoption">VersioningOption</a></td>
142
- <td>No</td>
143
- <td>undefine</td>
144
- <td>Versioning configuration</td>
145
- </tr>
146
- <tr>
147
- <td>timeout</td>
148
- <td>number</td>
149
- <td>No</td>
150
- <td>5000</td>
151
- <td>Global timeout (ms)</td>
152
- </tr>
153
- <tr>
154
- <td>cacheManage</td>
155
- <td>{type:CacheType,ttl:number}</td>
156
- <td>No</td>
157
- <td>{
158
- type: CacheType.Memory,
159
- ttl: 500
160
- }</td>
161
- <td>Cache manager (ttl in seconds)</td>
162
- </tr>
163
- <tr>
164
- <td>cacheFor</td>
165
- <td>RequestMethod | RequestMethod[] | 'All' | 'all' </td>
166
- <td>No</td>
167
- <td>Get</td>
168
- <td>Methods to enable caching</td>
169
- </tr>
170
- <tr>
171
- <td>serverStatusCodeRule</td>
172
- <td><a href="#SnailServerStatusCodeRuleOptions">SnailServerStatusCodeRuleOptions</a></td>
173
- <td>否</td>
174
- <td>undefined</td>
175
- <td>Server status code validation rules (triggers error when rule function returns false)</td>
176
- </tr>
177
- <tr>
178
- <td>enableLog</td>
179
- <td>boolean</td>
180
- <td>No</td>
181
- <td>false</td>
182
- <td>Enable debug logs</td>
183
- </tr>
184
- </table>
185
-
186
- ### API Configuration
187
- - Decorate API classes with `@Api()` and extend `SnailApi`
188
-
189
- <table>
190
- <tr>
191
- <th>Option</th>
192
- <th>Type</th>
193
- <th>Required</th>
194
- <th>Default Value</th>
195
- <th>Description</th>
196
- </tr>
197
- <tr>
198
- <td>name</td>
199
- <td>string</td>
200
- <td>No</td>
201
- <td>default use extends`SnailApi` class name</td>
202
- <td>Unique API identifier</td>
203
- </tr>
204
- <tr>
205
- <td>timeout</td>
206
- <td>number</td>
207
- <td>No</td>
208
- <td></td>
209
- <td>Overrides server timeout</td>
210
- </tr>
211
- <tr>
212
- <td>version</td>
213
- <td>string</td>
214
- <td>No</td>
215
- <td></td>
216
- <td>Overrides server defaultVersion</td>
217
- </tr>
218
- </table>
219
-
220
- ## Request Method Decorators
221
- - Used in API classes to mark request methods
222
- - Supports all axios methods: Get, Post, Head, Put, Delete, Patch, Options
223
- - Parameter: path?: string - Endpoint path (combined with baseURL)
224
-
225
- ## Parameter Decorators
226
- ### Query Parameters @Query
227
- - @Query(key?:string)
228
- Example:
229
-
230
- ```ts
231
- @Api("user")
232
- class UserApi {
233
- @Get()
234
- get(@Query("id") id: string, @Query("sign") sign: string) {}
235
- }
236
- ```
237
- > Parameters will be appended as ?k1=v1&k2=v2
238
-
239
- ### Route Parameters @Params
240
- - @Params(key?:string)
241
- Example with object:
242
- ```ts
243
- class RouteParams {
244
- id: string;
245
- sign: string;
246
- }
247
-
248
- @Api("user/:id/:sign")
249
- class UserApi {
250
- @Get()
251
- get(@Params() params: RouteParams) {}
252
- }
253
- ```
254
-
255
- > Automatically maps object properties to route parameters
256
-
257
- ### Request Data
258
-
259
- - `@Data(key?:string)`
260
- - Usage pattern same as `@Params`, supports mixed usage
261
-
262
- ## Strategy Decorator `@UseStrategy`
263
-
264
- - `@UseStrategy(...Strategy[])`
265
-
266
- ### Request Strategy
267
-
268
- - Executed before request sending, subsequent strategy results override previous ones
269
- - If returns processed request, uses it for sending; otherwise uses original or previous strategy's result
270
-
271
- ```typescript
272
- class CustomStrategy extends Strategy {
273
- applyRequest(request: AxiosRequestConfig) {
274
- request.headers["Access-Token"] = "abcde";
275
- return request;
276
- }
277
- }
278
-
279
- // Applied to Snail for global request strategies
280
- @Server({
281
- baseURL: "/api",
282
- timeout: 5000,
283
- })
284
- @UseStrategy(CustomStrategy)
285
- class BackEnd extends Snail<ShanheResponse> {}
286
- export const Service = new BackEnd();
287
- // Register strategy after instance creation
288
- Service.registerStrategies(CustomStrategy);
289
-
290
- // Applied to API for method-specific strategies
291
- @Api("test")
292
- @UseStrategy(CustomStrategy)
293
- class Test {}
294
- const TestApi = Service.createApi(Test);
295
- TestApi.registerStrategies(CustomStrategy);
296
-
297
- // Applied to method for endpoint-level strategies
298
- @Api("test")
299
- @UseStrategy(CustomStrategy)
300
- class Test {
301
- @Get()
302
- @UseStrategy(CustomStrategy)
303
- get() {}
304
- }
305
- // Register strategy before request
306
- const TestApi = Service.createApi(Test);
307
- const getSomething = TestApi.get();
308
- { send, registerStrategies } = getSomething;
309
- registerStrategies(CustomStrategy);
310
-
311
- ```
312
-
313
- ### Response Strategy
314
- - Executed after receiving server response
315
- - Processed response propagates through strategy chain
316
-
317
- ```typescript
318
- class CustomStrategy extends Strategy {
319
- applyResponse(response: AxiosResponse) {
320
- const { status } = response;
321
- if (status == 200) {
322
- // Custom processing
323
- }
324
- return response;
325
- }
326
- }
327
- ```
328
-
329
- ## Version Management Decorators @Versioning & @Version
330
- ### Version Manager @Versioning(VersioningOption)
331
- - Global version management
332
-
333
- ```typescript
334
- @Server({
335
- baseURL: "/api",
336
- timeout: 5000,
337
- })
338
- @Versioning({
339
- type: VersioningType.Header,
340
- defaultVersion: "0.1.0",
341
- })
342
- class BackEnd extends Snail<ShanheResponse> {}
343
-
344
- export const Service = new BackEnd();
345
- ```
346
-
347
- ### <a id='versioningOption'>VersioningOption Type</a>
348
-
349
- ```typescript
350
- export enum VersioningType {
351
- Uri,
352
- Header,
353
- Query,
354
- Custom,
355
- }
356
-
357
- interface VersioningCommonOption {
358
- defaultVersion: string;
359
- }
360
-
361
- export interface VersioningUriOption extends VersioningCommonOption {
362
- type: VersioningType.Uri;
363
- prefix?: string;
364
- }
365
-
366
- export interface VersioningHeaderOption extends VersioningCommonOption {
367
- type: VersioningType.Header;
368
- header?: string;
369
- }
370
-
371
- export interface VersioningQueryOption extends VersioningCommonOption {
372
- type: VersioningType.Query;
373
- key?: string;
374
- }
375
-
376
- export interface VersioningCustomOption extends VersioningCommonOption {
377
- type: VersioningType.Custom;
378
- extractor: (requestOptions: unknown) => {
379
- url: string;
380
- headers: Record<string, any>;
381
- };
382
- }
383
-
384
- export type VersioningOption =
385
- | VersioningUriOption
386
- | VersioningHeaderOption
387
- | VersioningQueryOption
388
- | VersioningCustomOption;
389
- ```
390
-
391
- ### <a id="SnailServerStatusCodeRuleOptions">SnailServerStatusCodeRuleOptions</a> Type
392
-
393
- ```typescript
394
- export class SnailServerStatusCodeRuleOptions {
395
- // Defines validation rules for server status codes
396
- // Triggers error when rule function returns false
397
- rule: (statusCode: number) => boolean;
398
- // Specifies the key in response.data for status code (default: 'code')
399
- key?: string;
400
- }
401
- ```
402
-
403
- ### Temporary Version Modifier @Version
404
- - Override version for specific methods
405
- ```typescript
406
- @Api("test")
407
- class Test {
408
- @Get("HelloWorld")
409
- @Version("0.2.0")
410
- test() {}
411
- }
412
- ```
413
-
414
- > Enables temporary version override for testing
415
-
416
- ### Cache Decorator @HitSource
417
- - @HitSource(name:string)
418
- - Defines cache invalidation sources
419
- - Name format: serverName:apiName:methodName
420
- > Note: Use configured names if available, otherwise class names
421
-
422
- ```typescript
423
- @Api("test",{name:'api1'})
424
- @HitSource("api1")
425
- class Test {
426
- @Get("HelloWorld")
427
- @HitSource("api1.test2")
428
- test1() {}
429
-
430
- @Post()
431
- test2() {}
432
-
433
- @Get()
434
- // Invalidates cache for all Test class methods
435
- @HitSource("api1")
436
- test3() {}
437
- }
438
- ```
439
-
440
- > Successful [Post]test calls invalidate [Get]test/HelloWorld cache Default caching only for GET methods. Use @Server({cacheFor:'all'}) for other methods
441
-
442
- > When `test3` method request succeeds, it won't be cached
443
-
444
- > Note: To enable caching, configure `@Server({cacheManage})` cache manager
445
-
446
- ### Upload Progress Decorator `@UploadProgress`
447
-
448
- - `@UploadProgress((progressEvent: AxiosProgressEvent) => void)`
449
-
450
- ### Download Progress Decorator `@DownloadProgress`
451
-
452
- - `@DownloadProgress((progressEvent: AxiosProgressEvent) => void)`
453
-
454
- ## Server-Sent Events (SSE)
455
-
456
- ### Create SSE Endpoint
457
-
458
- ```typescript
459
- @Sse("sse")
460
- class ServerSend extends SnailSse {
461
-
462
- @OnSseOpen()
463
- handleOpen(event: Event) {
464
- console.log("SSE connection opened:", event);
465
- }
466
-
467
- @OnSseError()
468
- handleError(event: Event) {
469
- console.log("SSE error occurred:", event);
470
- }
471
-
472
- // Handle default message events
473
- @SseEvent()
474
- handleEvent(event: MessageEvent) {
475
- console.log("SSE message event:", event.data);
476
- }
477
-
478
- // Handle custom named events
479
- @SseEvent("chunk")
480
- handleChunkEvent(event: Event) {
481
- console.log("SSE chunk event:", event);
482
- }
483
- }
484
-
485
- export const Sse = Service.createSse(ServerSend);
486
- ```
487
-
488
- ### SSE Decorator @Sse
489
- - @Sse(path: string, options?: { withCredentials?: boolean, version?: string })
490
- - Creates a server-sent events connection, returns a function to open SSE connection:
491
- - Returns { eventSource: EventSource, close: () => void } when called:
492
- - eventSource: SSE connection instance
493
- - close: Method to close the connection
494
- ### SSE Open Handler Decorator @OnSseOpen
495
- - Registers decorated method as onopen handler for EventSource instances created by @Sse decorated methods
496
- ### SSE Error Handler Decorator @OnSseError
497
- - Registers decorated method as onerror handler for EventSource instances created by @Sse decorated methods
498
- ### SSE Event Handler Decorator @SseEvent
499
- - @SseEvent(eventName?: string)
500
- - Without eventName : Registers as default message event handler
501
- - With eventName : Registers as handler for specified custom event
502
-
503
- ------
504
-
505
- ## TypeScript Support
506
- ### Default Response Type
507
-
508
- ```typescript
509
- export type StandardResponseData<
510
- T extends ResponseJsonData = Record<string, any>
511
- > = {
512
- code: number;
513
- message: string;
514
- data: T;
515
- };
516
- ```
517
-
518
- ### Custom Response Types
519
- 1. Define backend response format:
520
- ```typescript
521
- export class CustomResponse {
522
- status_code: number;
523
- msg: string;
524
- }
525
- ```
526
- 2. Apply type when creating service:
527
- ```typescript
528
- // service.ts
529
- import { SnailServer, Server } from "@snail-js/api";
530
-
531
- @Server({
532
- baseURL: "/api",
533
- timeout: 5000,
534
- })
535
- class BackEnd extends SnailServer<CustomResponse> {}
536
-
537
- export const Service = new BackEnd();
538
- ```
539
-
540
- 3. Type annotation when calling APIs:
541
- ```typescript
542
- import { userApi } from "./user";
543
-
544
- class User {
545
- id: number;
546
- name: string;
547
- tel: string;
548
- age: number;
549
- }
550
-
551
- const getUser = userApi.get<User>();
552
- const { send } = getUser;
553
-
554
- const res = await send("1");
555
-
556
- // Default data key:
557
- // res.data => CustomResponse & { data: User }
558
- ```
559
-
560
- > API response format:
561
- ```typescript
562
- const getUser = userApi.get<User>();
563
- const { send } = getUser;
564
- const res = await send("1");
565
-
566
- // res.data => CustomResponse & { data: User }
567
-
568
- const getUser = userApi.get<Blob>();
569
- const { send } = getUser;
570
- const res = await send("1");
571
- // res => AxiosResponse<Blob>
572
- ```
573
-
574
- 4. Custom data key configuration:
575
-
576
- ```typescript
577
- @Server({
578
- baseURL: "/api",
579
- timeout: 5000,
580
- })
581
- class BackEnd extends Snail<CustomResponse, "record"> {}
582
-
583
- const { send } = userApi.get<User>();
584
- const res = await send("1");
585
- // Custom data key:
586
- // res.data => CustomResponse & { record: User }
587
- ```
588
-
589
- ### Non-JSON Responses
590
- - For non-JSON content-type responses: send() returns AxiosResponse
591
- - For JSON content-type responses: send() returns AxiosResponse.data
592
-
593
- ### Repository
594
- <p>
595
- <a href="https://gitee.com/limich/snail">
596
- <img src="https://img.shields.io/badge/snail-js?style=flat&label=gitee&labelColor=F56C6C&link=https%3A%2F%2Fgitee.com%2Flimich%2Fsnail"></img>
597
- </a>
598
- </p>
599
- <p>
600
- <a href="https://github.com/limingchang/snail">
601
- <img src="https://img.shields.io/badge/snail-js?style=flat&label=github&labelColor=F56C6C&link=https%3A%2F%2Fgihub.com%2Flimingchang%2Fsnail"></img>
602
- </a>
603
- </p>
604
-
605
- ### Author
606
-
607
- - mc.lee
@@ -1,9 +0,0 @@
1
- import MemoryCache from "./memoryCache";
2
- import LocalStorageCache from "./localstorageCache";
3
- import IndexDBCache from "./indexDBCache";
4
- import { CacheStorageAdapter, CacheStorage, MemoryCacheOption, IndexDBCacheOption, LocalStorageCacheOption, CustomCacheOption, CacheManagementOption } from "../typings";
5
- export declare function createCache<T extends CacheManagementOption>(options: T): CacheStorage;
6
- export declare function createCache<T extends MemoryCacheOption>(optios: T): MemoryCache;
7
- export declare function createCache<T extends LocalStorageCacheOption>(optios: T): LocalStorageCache;
8
- export declare function createCache<T extends IndexDBCacheOption>(optios: T): IndexDBCache;
9
- export declare function createCache<T extends CustomCacheOption>(optios: T): CacheStorageAdapter;
@@ -1,13 +0,0 @@
1
- import { CacheGetData, CacheStorageAdapter } from "../typings";
2
- export default class IndexDBCache implements CacheStorageAdapter {
3
- ttl: number;
4
- private db?;
5
- constructor(ttl: number);
6
- init(): Promise<void>;
7
- private openDB;
8
- get<T = any>(key: string): Promise<CacheGetData<T>>;
9
- set<T = any>(key: string, value: T): Promise<boolean>;
10
- delete(key: string): Promise<boolean>;
11
- clear(): Promise<boolean>;
12
- keys(): Promise<string[]>;
13
- }
@@ -1,10 +0,0 @@
1
- import { CacheGetData, CacheStorageAdapter } from "../typings";
2
- export default class LocalStorageCache implements CacheStorageAdapter {
3
- ttl: number;
4
- constructor(ttl: number);
5
- get<T = any>(key: string): Promise<CacheGetData<T>>;
6
- set<T = any>(key: string, value: T): Promise<boolean>;
7
- delete(key: string): Promise<boolean>;
8
- clear(): Promise<boolean>;
9
- keys(): Promise<string[]>;
10
- }
@@ -1,11 +0,0 @@
1
- import { CacheGetData, CacheStorageAdapter } from "../typings";
2
- export default class MemoryCache implements CacheStorageAdapter {
3
- private CacheMap;
4
- ttl: number;
5
- constructor(ttl: number);
6
- get<T = any>(key: string): Promise<CacheGetData<T>>;
7
- set<T = any>(key: string, value: T): Promise<boolean>;
8
- delete(key: string): Promise<boolean>;
9
- clear(): Promise<boolean>;
10
- keys(): Promise<string[]>;
11
- }
@@ -1,4 +0,0 @@
1
- export { SnailServer } from "./snailServer";
2
- export { SnailMethod } from "./snailMethod";
3
- export { SnailApi } from "./snailApi";
4
- export { SnailSse } from "./snailSse";
@@ -1,22 +0,0 @@
1
- import { Strategy, ApiInstanceOptions } from "../typings";
2
- export declare class SnailApi {
3
- private Name;
4
- private serverInstance;
5
- private Version?;
6
- private Url?;
7
- private Timeout?;
8
- private EnableLog?;
9
- constructor(options: ApiInstanceOptions);
10
- private init;
11
- private initStrategy;
12
- registerStrategies: (...strategys: Array<new () => Strategy>) => void;
13
- private initName;
14
- private getApiConfig;
15
- private isNoCache;
16
- get version(): string | undefined;
17
- get url(): string | undefined;
18
- get name(): string;
19
- get noCache(): boolean;
20
- get enableLog(): boolean | undefined;
21
- get timeout(): number | undefined;
22
- }