@agentuity/server 0.0.35 → 0.0.36

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 (44) hide show
  1. package/dist/api/api.d.ts +53 -0
  2. package/dist/api/api.d.ts.map +1 -0
  3. package/dist/api/api.js +178 -0
  4. package/dist/api/api.js.map +1 -0
  5. package/dist/api/index.d.ts +4 -0
  6. package/dist/api/index.d.ts.map +1 -0
  7. package/dist/api/index.js +4 -0
  8. package/dist/api/index.js.map +1 -0
  9. package/dist/api/org/index.d.ts +2 -0
  10. package/dist/api/org/index.d.ts.map +1 -0
  11. package/dist/api/org/index.js +2 -0
  12. package/dist/api/org/index.js.map +1 -0
  13. package/dist/api/org/list.d.ts +21 -0
  14. package/dist/api/org/list.d.ts.map +1 -0
  15. package/dist/api/org/list.js +16 -0
  16. package/dist/api/org/list.js.map +1 -0
  17. package/dist/api/project/create.d.ts +28 -0
  18. package/dist/api/project/create.d.ts.map +1 -0
  19. package/dist/api/project/create.js +27 -0
  20. package/dist/api/project/create.js.map +1 -0
  21. package/dist/api/project/exists.d.ts +23 -0
  22. package/dist/api/project/exists.d.ts.map +1 -0
  23. package/dist/api/project/exists.js +61 -0
  24. package/dist/api/project/exists.js.map +1 -0
  25. package/dist/api/project/index.d.ts +3 -0
  26. package/dist/api/project/index.d.ts.map +1 -0
  27. package/dist/api/project/index.js +3 -0
  28. package/dist/api/project/index.js.map +1 -0
  29. package/dist/index.d.ts +2 -0
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +2 -0
  32. package/dist/index.js.map +1 -1
  33. package/package.json +4 -2
  34. package/src/api/api-example.md +155 -0
  35. package/src/api/api.ts +241 -0
  36. package/src/api/index.ts +3 -0
  37. package/src/api/org/index.ts +1 -0
  38. package/src/api/org/list.ts +28 -0
  39. package/src/api/project/create.ts +43 -0
  40. package/src/api/project/exists.ts +76 -0
  41. package/src/api/project/index.ts +2 -0
  42. package/src/config.ts +20 -0
  43. package/src/index.ts +14 -0
  44. package/src/server.ts +133 -0
package/src/server.ts ADDED
@@ -0,0 +1,133 @@
1
+ import type {
2
+ FetchRequest,
3
+ FetchErrorResponse,
4
+ FetchResponse,
5
+ FetchAdapter,
6
+ } from '@agentuity/core';
7
+ import { ServiceException, toServiceException, fromResponse } from '@agentuity/core';
8
+
9
+ interface ServiceAdapterConfig {
10
+ headers: Record<string, string>;
11
+ onBefore?: (url: string, options: FetchRequest, invoke: () => Promise<void>) => Promise<void>;
12
+ onAfter?: <T>(
13
+ url: string,
14
+ options: FetchRequest,
15
+ response: FetchResponse<T>,
16
+ err?: ServiceException
17
+ ) => Promise<void>;
18
+ }
19
+
20
+ class ServerFetchAdapter implements FetchAdapter {
21
+ #config: ServiceAdapterConfig;
22
+
23
+ constructor(config: ServiceAdapterConfig) {
24
+ this.#config = config;
25
+ }
26
+ private async _invoke<T>(url: string, options: FetchRequest): Promise<FetchResponse<T>> {
27
+ const headers = {
28
+ ...options.headers,
29
+ ...this.#config.headers,
30
+ };
31
+ if (options.contentType) {
32
+ headers['Content-Type'] = options.contentType;
33
+ } else if (
34
+ typeof options.body === 'string' ||
35
+ options.body instanceof Uint8Array ||
36
+ options.body instanceof ArrayBuffer
37
+ ) {
38
+ headers['Content-Type'] = 'application/octet-stream';
39
+ }
40
+ const res = await fetch(url, {
41
+ method: options.method ?? 'POST',
42
+ body: options.body,
43
+ headers,
44
+ signal: options.signal,
45
+ ...(options.duplex ? { duplex: options.duplex } : {}),
46
+ });
47
+ if (res.ok) {
48
+ switch (res.status) {
49
+ case 100:
50
+ case 101:
51
+ case 102:
52
+ case 204:
53
+ case 304:
54
+ return {
55
+ ok: true,
56
+ data: undefined as T,
57
+ response: res,
58
+ };
59
+ default:
60
+ break;
61
+ }
62
+ if (options?.binary) {
63
+ return {
64
+ ok: true,
65
+ data: undefined as T,
66
+ response: res,
67
+ };
68
+ }
69
+ const data = await fromResponse<T>(res);
70
+ return {
71
+ ok: true,
72
+ data,
73
+ response: res,
74
+ };
75
+ }
76
+ if (res.status === 404) {
77
+ return {
78
+ ok: false,
79
+ response: res,
80
+ } as FetchErrorResponse;
81
+ }
82
+ const err = await toServiceException(res);
83
+ throw err;
84
+ }
85
+ async invoke<T>(
86
+ url: string,
87
+ options: FetchRequest = { method: 'POST' }
88
+ ): Promise<FetchResponse<T>> {
89
+ if (this.#config.onBefore) {
90
+ let result: FetchResponse<T> | undefined = undefined;
91
+ let err: Error | undefined = undefined;
92
+ await this.#config.onBefore(url, options, async () => {
93
+ try {
94
+ result = await this._invoke(url, options);
95
+ if (this.#config.onAfter) {
96
+ await this.#config.onAfter(url, options, result);
97
+ }
98
+ } catch (ex) {
99
+ err = ex as Error;
100
+ if (this.#config.onAfter) {
101
+ await this.#config.onAfter(
102
+ url,
103
+ options,
104
+ {
105
+ ok: false,
106
+ response: new Response((err as ServiceException).message ?? String(err), {
107
+ status: (err as ServiceException).statusCode ?? 500,
108
+ }),
109
+ } as FetchErrorResponse,
110
+ err as ServiceException
111
+ );
112
+ }
113
+ }
114
+ });
115
+ if (err) {
116
+ throw err;
117
+ }
118
+ return result as unknown as FetchResponse<T>;
119
+ } else {
120
+ return await this._invoke(url, options);
121
+ }
122
+ }
123
+ }
124
+
125
+ /**
126
+ * Create a Server Side Fetch Adapter to allow the server to add headers and track outgoing requests
127
+ *
128
+ * @param config the service config
129
+ * @returns
130
+ */
131
+ export function createServerFetchAdapter(config: ServiceAdapterConfig) {
132
+ return new ServerFetchAdapter(config);
133
+ }