@superblocksteam/sdk 1.8.2 → 1.9.1

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/src/sdk.ts CHANGED
@@ -5,22 +5,23 @@ import {
5
5
  } from "@superblocksteam/util";
6
6
  import {
7
7
  fetchApi,
8
+ fetchApiCommits,
8
9
  fetchApis,
9
10
  fetchApplication,
10
11
  fetchApplicationBranches,
12
+ fetchApplicationCommits,
11
13
  fetchApplications,
12
14
  fetchApplicationWithComponents,
13
15
  fetchCurrentUser,
14
16
  pushApi,
15
17
  PushApiWithCommitConfig,
16
18
  pushApplication,
17
- PushApplicationWithCommitConfig,
18
19
  PushMultiPageApplicationWithCommitConfig,
19
20
  registerComponents,
20
21
  uploadComponents,
21
22
  validateGitSetup,
22
- ViewMode,
23
23
  } from "./client";
24
+ import { ViewMode } from "./types";
24
25
 
25
26
  // Exporting here instead of index.ts because only sdk.ts is exposed outside in package.json
26
27
  export * from "./errors";
@@ -45,13 +46,13 @@ export class SuperblocksSdk {
45
46
  branch,
46
47
  headers = {},
47
48
  viewMode = "export-live",
48
- fetchSinglePageApplication = false,
49
+ skipSigningVerification = false,
49
50
  }: {
50
51
  applicationId: string;
51
52
  branch?: string;
52
53
  headers?: Record<string, string>;
53
54
  viewMode?: ViewMode;
54
- fetchSinglePageApplication?: boolean;
55
+ skipSigningVerification?: boolean;
55
56
  }) {
56
57
  return fetchApplication({
57
58
  cliVersion: this.cliVersion,
@@ -60,8 +61,8 @@ export class SuperblocksSdk {
60
61
  token: this.token,
61
62
  superblocksBaseUrl: this.superblocksBaseUrl,
62
63
  viewMode,
64
+ skipSigningVerification,
63
65
  injectedHeaders: headers,
64
- fetchSinglePageApplication,
65
66
  });
66
67
  }
67
68
 
@@ -80,13 +81,15 @@ export class SuperblocksSdk {
80
81
  branch,
81
82
  headers = {},
82
83
  viewMode = "export-live",
83
- fetchSinglePageApplication = false,
84
+ commitId,
85
+ skipSigningVerification = false,
84
86
  }: {
85
87
  applicationId: string;
86
88
  branch: string;
87
89
  headers?: Record<string, string>;
88
90
  viewMode?: ViewMode;
89
- fetchSinglePageApplication?: boolean;
91
+ commitId?: string;
92
+ skipSigningVerification?: boolean;
90
93
  }) {
91
94
  return fetchApplicationWithComponents({
92
95
  cliVersion: this.cliVersion,
@@ -95,8 +98,59 @@ export class SuperblocksSdk {
95
98
  token: this.token,
96
99
  superblocksBaseUrl: this.superblocksBaseUrl,
97
100
  viewMode,
101
+ skipSigningVerification,
102
+ commitId,
98
103
  injectedHeaders: headers,
99
- fetchSinglePageApplication,
104
+ });
105
+ }
106
+
107
+ async fetchApplicationCommits({
108
+ applicationId,
109
+ branch,
110
+ headers = {},
111
+ limit,
112
+ offset,
113
+ }: {
114
+ applicationId: string;
115
+ branch: string;
116
+ headers?: Record<string, string>;
117
+ limit?: number;
118
+ offset?: number;
119
+ }) {
120
+ return fetchApplicationCommits({
121
+ cliVersion: this.cliVersion,
122
+ applicationId,
123
+ branch,
124
+ token: this.token,
125
+ superblocksBaseUrl: this.superblocksBaseUrl,
126
+ injectedHeaders: headers,
127
+ limit,
128
+ offset,
129
+ });
130
+ }
131
+
132
+ async fetchApiCommits({
133
+ apiId,
134
+ branch,
135
+ headers = {},
136
+ limit,
137
+ offset,
138
+ }: {
139
+ apiId: string;
140
+ branch: string;
141
+ headers?: Record<string, string>;
142
+ limit?: number;
143
+ offset?: number;
144
+ }) {
145
+ return fetchApiCommits({
146
+ cliVersion: this.cliVersion,
147
+ applicationId: apiId,
148
+ branch,
149
+ token: this.token,
150
+ superblocksBaseUrl: this.superblocksBaseUrl,
151
+ injectedHeaders: headers,
152
+ limit,
153
+ offset,
100
154
  });
101
155
  }
102
156
 
@@ -109,18 +163,28 @@ export class SuperblocksSdk {
109
163
  );
110
164
  }
111
165
 
112
- async fetchApi(
113
- apiId: string,
114
- viewMode: ViewMode = "export-live",
115
- branch?: string
116
- ) {
166
+ async fetchApi({
167
+ apiId,
168
+ branch,
169
+ viewMode = "export-live",
170
+ commitId,
171
+ skipSigningVerification = false,
172
+ }: {
173
+ apiId: string;
174
+ viewMode?: ViewMode;
175
+ branch?: string;
176
+ commitId?: string;
177
+ skipSigningVerification?: boolean;
178
+ }) {
117
179
  return fetchApi(
118
180
  this.cliVersion,
119
181
  apiId,
120
182
  this.token,
121
183
  this.superblocksBaseUrl,
122
184
  viewMode,
123
- branch
185
+ branch,
186
+ commitId,
187
+ skipSigningVerification
124
188
  );
125
189
  }
126
190
 
@@ -194,15 +258,11 @@ export class SuperblocksSdk {
194
258
  applicationConfig,
195
259
  branch,
196
260
  headers = {},
197
- multiPage = false,
198
261
  }: {
199
262
  applicationId: string;
200
- applicationConfig:
201
- | PushApplicationWithCommitConfig
202
- | PushMultiPageApplicationWithCommitConfig;
263
+ applicationConfig: PushMultiPageApplicationWithCommitConfig;
203
264
  branch: string;
204
265
  headers?: Record<string, string>;
205
- multiPage?: boolean;
206
266
  }) {
207
267
  return pushApplication({
208
268
  cliVersion: this.cliVersion,
@@ -212,7 +272,6 @@ export class SuperblocksSdk {
212
272
  branch,
213
273
  injectedHeaders: headers,
214
274
  applicationConfig,
215
- multiPage,
216
275
  });
217
276
  }
218
277
  async pushApi({
@@ -7,6 +7,7 @@ import {
7
7
  MethodHandlers,
8
8
  RemoteCommitDto,
9
9
  Signature,
10
+ ViewMode,
10
11
  } from "../types";
11
12
  import { signResource, verifyResources } from "./signing";
12
13
 
@@ -108,12 +109,22 @@ export interface ServerMethods {
108
109
  {
109
110
  apiId: string;
110
111
  branchName: string;
111
- commitId: string;
112
- commitMessage: string;
112
+ commitId?: string;
113
+ commitMessage?: string;
113
114
  apiPb: Record<string, unknown>;
114
115
  gitState: LocalGitRepoState;
116
+ skipCommit: boolean;
115
117
  },
116
- RemoteCommitDto
118
+ RemoteCommitDto | { updated: Date }
119
+ >;
120
+ get: ServerMethodSchema<
121
+ {
122
+ apiId: string;
123
+ branchName?: string;
124
+ viewMode: ViewMode;
125
+ commitId?: string;
126
+ },
127
+ any
117
128
  >;
118
129
  };
119
130
  };
@@ -125,14 +136,24 @@ export interface ServerMethods {
125
136
  {
126
137
  applicationId: string;
127
138
  branchName: string;
128
- commitId: string;
129
- commitMessage: string;
139
+ commitId?: string;
140
+ commitMessage?: string;
130
141
  application: Record<string, unknown>;
131
142
  pages: Record<string, unknown>[];
132
143
  apis: Record<string, unknown>[];
133
144
  gitState: LocalGitRepoState;
145
+ skipCommit: boolean;
134
146
  },
135
- RemoteCommitDto
147
+ RemoteCommitDto | { updated: Date }
148
+ >;
149
+ get: ServerMethodSchema<
150
+ {
151
+ applicationId: string;
152
+ viewMode: ViewMode;
153
+ branchName?: string;
154
+ commitId?: string;
155
+ },
156
+ any
136
157
  >;
137
158
  };
138
159
  };
@@ -1,4 +1,11 @@
1
1
  import WebSocket from "ws";
2
+ import {
3
+ GenericMiddleware,
4
+ ISocketClient,
5
+ MethodHandlers,
6
+ RequestContextBase,
7
+ SocketTimeouts,
8
+ } from "../types";
2
9
  import {
3
10
  ClientMethods,
4
11
  createRequestHandlers,
@@ -34,20 +41,26 @@ export async function connectToISocketRPCServer({
34
41
  } else if (wsUrl.hostname === "localhost") {
35
42
  wsUrl.hostname = "127.0.0.1";
36
43
  }
37
- return await connectISocket<ServerMethods, ClientMethods, unknown>(
44
+
45
+ return await connectISocket<ServerMethods, ClientMethods>(
38
46
  wsUrl.href,
39
47
  authorization,
40
- requestHandlers
48
+ requestHandlers,
49
+ [],
50
+ {
51
+ connectionTimeoutInSeconds: 6 * 60, // 6 minutes
52
+ noResponseTimeoutInSeconds: 5 * 60, // 5 minutes
53
+ }
41
54
  );
42
55
  }
43
56
 
44
57
  // a subclass of ISocket that sends an auth token on the first request
45
58
  // this is useful for client-side sockets that need to authenticate
46
59
  // TODO(george): if we start using this for long-lived connections, we should add a way to refresh the token
47
- class ISocketWithClientAuth<
60
+ export class ISocketWithClientAuth<
48
61
  ImplementedMethods,
49
62
  CallableMethods,
50
- RequestContext = void
63
+ RequestContext extends RequestContextBase
51
64
  > extends ISocket<ImplementedMethods, CallableMethods, RequestContext> {
52
65
  private readonly authorization?: string;
53
66
  private hasSentAuth = false;
@@ -59,9 +72,11 @@ class ISocketWithClientAuth<
59
72
  ImplementedMethods,
60
73
  CallableMethods,
61
74
  RequestContext
62
- >
75
+ >,
76
+ globalMiddlewares: GenericMiddleware<CallableMethods, RequestContext>[],
77
+ timeouts?: SocketTimeouts
63
78
  ) {
64
- super(ws, requestHandlers);
79
+ super(ws, requestHandlers, globalMiddlewares, timeouts);
65
80
  this.authorization = authorization;
66
81
  }
67
82
 
@@ -85,7 +100,7 @@ class ISocketWithClientAuth<
85
100
  export async function connectISocket<
86
101
  CallableMethods,
87
102
  ImplementedMethods,
88
- RequestContext = never
103
+ RequestContext extends RequestContextBase = RequestContextBase
89
104
  >(
90
105
  wsUrl: string,
91
106
  authorization: string | undefined,
@@ -93,10 +108,18 @@ export async function connectISocket<
93
108
  ImplementedMethods,
94
109
  CallableMethods,
95
110
  RequestContext
96
- >
111
+ >,
112
+ globalMiddlewares: GenericMiddleware<CallableMethods, RequestContext>[],
113
+ timeouts?: SocketTimeouts
97
114
  ): Promise<ISocketClient<CallableMethods>> {
98
115
  const ws = await connectWebSocket(wsUrl);
99
- const isocket = new ISocketWithClientAuth(ws, authorization, requestHandlers);
116
+ const isocket = new ISocketWithClientAuth(
117
+ ws,
118
+ authorization,
119
+ requestHandlers,
120
+ globalMiddlewares,
121
+ timeouts
122
+ );
100
123
  return createISocketClient(isocket);
101
124
  }
102
125
 
@@ -117,48 +140,3 @@ export function connectWebSocket(wsUrl: string): Promise<WebSocket> {
117
140
  });
118
141
  });
119
142
  }
120
-
121
- type MethodHandler<Params, Result, PeerMethods, RequestContext> = (
122
- params: Params,
123
- peer: ISocketClient<PeerMethods>,
124
- ctx: RequestContext
125
- ) => Promise<Result>;
126
-
127
- type MiddlewareHandler<Params, PeerMethods, RequestContext> = (
128
- params: Params,
129
- peerAuthorization: string | undefined,
130
- peer: ISocketClient<PeerMethods>,
131
- ctx: RequestContext
132
- ) => Promise<void>;
133
-
134
- type MethodHandlers<Methods, PeerMethods, RequestContext> = {
135
- [Key in keyof Methods]: Methods[Key] extends (
136
- params: infer Params
137
- ) => Promise<infer Result>
138
- ? [
139
- ...middlewareHandlers: MiddlewareHandler<
140
- Params,
141
- PeerMethods,
142
- RequestContext
143
- >[],
144
- handler: MethodHandler<Params, Result, PeerMethods, RequestContext>
145
- ]
146
- : Methods[Key] extends Record<string, unknown>
147
- ? MethodHandlers<Methods[Key], PeerMethods, RequestContext>
148
- : never;
149
- };
150
-
151
- type ISocketClientMethodCall<Methods> = {
152
- [Key in keyof Methods]: Methods[Key] extends (
153
- params: infer P
154
- ) => Promise<infer R>
155
- ? (params: P) => Promise<R>
156
- : Methods[Key] extends Record<string, unknown>
157
- ? ISocketClientMethodCall<Methods[Key]>
158
- : never;
159
- };
160
-
161
- type ISocketClient<Methods> = {
162
- close: () => void;
163
- call: ISocketClientMethodCall<Methods>;
164
- };