@slicemachine/manager 0.1.1-dev-plugins-m1-validation.0 → 0.1.1-dev-plugins-m2-validation.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.
Files changed (33) hide show
  1. package/dist/auth/createPrismicAuthManagerMiddleware.cjs +2 -1
  2. package/dist/auth/createPrismicAuthManagerMiddleware.cjs.map +1 -1
  3. package/dist/auth/createPrismicAuthManagerMiddleware.js +2 -1
  4. package/dist/auth/createPrismicAuthManagerMiddleware.js.map +1 -1
  5. package/dist/errors.cjs +5 -1
  6. package/dist/errors.cjs.map +1 -1
  7. package/dist/errors.js +5 -1
  8. package/dist/errors.js.map +1 -1
  9. package/dist/managers/SliceMachineManager.cjs +35 -18
  10. package/dist/managers/SliceMachineManager.cjs.map +1 -1
  11. package/dist/managers/SliceMachineManager.d.ts +1 -0
  12. package/dist/managers/SliceMachineManager.js +35 -18
  13. package/dist/managers/SliceMachineManager.js.map +1 -1
  14. package/dist/managers/prismicRepository/PrismicRepositoryManager.cjs +22 -21
  15. package/dist/managers/prismicRepository/PrismicRepositoryManager.cjs.map +1 -1
  16. package/dist/managers/prismicRepository/PrismicRepositoryManager.js +22 -21
  17. package/dist/managers/prismicRepository/PrismicRepositoryManager.js.map +1 -1
  18. package/dist/managers/simulator/SimulatorManager.cjs +5 -0
  19. package/dist/managers/simulator/SimulatorManager.cjs.map +1 -1
  20. package/dist/managers/simulator/SimulatorManager.d.ts +1 -0
  21. package/dist/managers/simulator/SimulatorManager.js +5 -0
  22. package/dist/managers/simulator/SimulatorManager.js.map +1 -1
  23. package/dist/managers/telemetry/TelemetryManager.cjs +3 -3
  24. package/dist/managers/telemetry/TelemetryManager.cjs.map +1 -1
  25. package/dist/managers/telemetry/TelemetryManager.js +3 -3
  26. package/dist/managers/telemetry/TelemetryManager.js.map +1 -1
  27. package/package.json +5 -5
  28. package/src/auth/createPrismicAuthManagerMiddleware.ts +3 -1
  29. package/src/errors.ts +11 -3
  30. package/src/managers/SliceMachineManager.ts +49 -22
  31. package/src/managers/prismicRepository/PrismicRepositoryManager.ts +28 -26
  32. package/src/managers/simulator/SimulatorManager.ts +10 -0
  33. package/src/managers/telemetry/TelemetryManager.ts +3 -3
@@ -1,5 +1,9 @@
1
1
  import { CustomTypes } from "@prismicio/types-internal";
2
2
  import { SharedSliceContent } from "@prismicio/types-internal/lib/content";
3
+ import {
4
+ ForbiddenError,
5
+ UnauthorizedError,
6
+ } from "@prismicio/custom-types-client";
3
7
  import {
4
8
  SliceMachinePlugin,
5
9
  SliceMachinePluginRunner,
@@ -44,6 +48,7 @@ type SliceMachineManagerGetStateReturnType = {
44
48
  repo: string;
45
49
  changelog?: PackageChangelog;
46
50
  packageManager: PackageManager;
51
+ supportsSliceSimulator: boolean;
47
52
  };
48
53
  libraries: {
49
54
  name: string;
@@ -163,7 +168,7 @@ export class SliceMachineManager {
163
168
  async getState(): Promise<SliceMachineManagerGetStateReturnType> {
164
169
  const [
165
170
  { sliceMachineConfig, libraries },
166
- { profile, remoteCustomTypes, remoteSlices },
171
+ { profile, remoteCustomTypes, remoteSlices, authError },
167
172
  customTypes,
168
173
  packageManager,
169
174
  ] = await Promise.all([
@@ -173,42 +178,63 @@ export class SliceMachineManager {
173
178
  return { sliceMachineConfig, libraries };
174
179
  }),
175
180
  this._getProfile().then(async (profile) => {
181
+ let authError;
176
182
  if (profile) {
177
- const [remoteCustomTypes, remoteSlices] = await Promise.all([
178
- this.customTypes.fetchRemoteCustomTypes(),
179
- this.slices.fetchRemoteSlices(),
180
- ]);
181
-
182
- return {
183
- profile,
184
- remoteCustomTypes,
185
- remoteSlices,
186
- };
187
- } else {
188
- return {
189
- profile,
190
- remoteCustomTypes: [],
191
- remoteSlices: [],
192
- };
183
+ try {
184
+ const [remoteCustomTypes, remoteSlices] = await Promise.all([
185
+ this.customTypes.fetchRemoteCustomTypes(),
186
+ this.slices.fetchRemoteSlices(),
187
+ ]);
188
+
189
+ return {
190
+ profile,
191
+ remoteCustomTypes,
192
+ remoteSlices,
193
+ authError,
194
+ };
195
+ } catch (error) {
196
+ // Non-Prismic error
197
+ if (
198
+ error instanceof UnauthorizedError ||
199
+ error instanceof ForbiddenError
200
+ ) {
201
+ authError = {
202
+ name: "__stub__",
203
+ message: "__stub__",
204
+ reason: "__stub__",
205
+ status: 401,
206
+ };
207
+ } else {
208
+ throw error;
209
+ }
210
+ }
193
211
  }
212
+
213
+ return {
214
+ profile,
215
+ remoteCustomTypes: [],
216
+ remoteSlices: [],
217
+ authError,
218
+ };
194
219
  }),
195
220
  this._getCustomTypes(),
196
221
  this.project.detectPackageManager(),
197
222
  ]);
198
223
 
199
- // TODO: SM UI detects if a user is logged out by looking at
224
+ // SM UI detects if a user is logged out by looking at
200
225
  // `clientError`. Here, we simulate what the old core does by
201
- // returning an `ErrorWithStatus`-like object if the user is
202
- // not logged in.
226
+ // returning an `ErrorWithStatus`-like object if the user does
227
+ // not have access to the repository or is not logged in.
203
228
  const clientError: SliceMachineManagerGetStateReturnType["clientError"] =
204
- profile
229
+ authError ||
230
+ (profile
205
231
  ? undefined
206
232
  : {
207
233
  name: "__stub__",
208
234
  message: "__stub__",
209
235
  reason: "__stub__",
210
236
  status: 401, // Needed to trigger the unauthorized flow.
211
- };
237
+ });
212
238
 
213
239
  return {
214
240
  env: {
@@ -221,6 +247,7 @@ export class SliceMachineManager {
221
247
  localSliceSimulatorURL: sliceMachineConfig.localSliceSimulatorURL,
222
248
  },
223
249
  packageManager,
250
+ supportsSliceSimulator: this.simulator.supportsSliceSimulator(),
224
251
  repo: sliceMachineConfig.repositoryName,
225
252
  intercomHash: profile?.intercomHash,
226
253
  shortId: profile?.shortId,
@@ -371,42 +371,39 @@ export class PrismicRepositoryManager extends BaseManager {
371
371
  changes: allChanges,
372
372
  };
373
373
 
374
- const authenticationToken = await this.user.getAuthenticationToken();
375
374
  const sliceMachineConfig = await this.project.getSliceMachineConfig();
376
375
 
377
- // TODO: API route in consts ends with /customtypes
378
376
  // TODO: move to customtypes client
379
- return fetch("https://customtypes.prismic.io/bulk", {
380
- body: JSON.stringify(requestBody),
377
+ const response = await this._fetch({
378
+ url: new URL("/bulk", API_ENDPOINTS.PrismicModels),
381
379
  method: "POST",
382
- headers: {
383
- Authorization: `Bearer ${authenticationToken}`,
384
- "User-Agent": "slice-machine",
385
- repository: sliceMachineConfig.repositoryName,
386
- "Content-Type": "application/json",
387
- },
388
- })
389
- .then(async (response) => {
390
- if (response.status === 204) {
391
- return null;
392
- }
380
+ body: requestBody,
381
+ repository: sliceMachineConfig.repositoryName,
382
+ });
393
383
 
384
+ switch (response.status) {
385
+ case 202:
394
386
  return this._decodeLimitOrThrow(
395
387
  await response.json(),
396
388
  response.status,
397
389
  LimitType.SOFT,
398
390
  );
399
- })
400
- .catch((err: ClientError) => {
401
- // Try to decode a limit from the error or throw the original error
402
- try {
403
- const data: unknown = JSON.parse(err.message);
404
-
405
- return this._decodeLimitOrThrow(data, err.status, LimitType.HARD);
406
- } catch {
407
- throw err;
408
- }
409
- });
391
+ case 204:
392
+ return null;
393
+ case 401:
394
+ throw new UnauthenticatedError();
395
+ case 403:
396
+ return this._decodeLimitOrThrow(
397
+ await response.json(),
398
+ response.status,
399
+ LimitType.HARD,
400
+ );
401
+ case 400:
402
+ const text = await response.text();
403
+ throw new Error(text);
404
+ default:
405
+ throw new Error(`Unexpected status code ${response.status}`);
406
+ }
410
407
  } catch (err) {
411
408
  console.error("An error happened while pushing your changes");
412
409
  console.error(err);
@@ -443,6 +440,7 @@ export class PrismicRepositoryManager extends BaseManager {
443
440
  method?: "GET" | "POST";
444
441
  body?: unknown;
445
442
  userAgent?: PrismicRepositoryUserAgents;
443
+ repository?: string;
446
444
  }): Promise<Response> {
447
445
  const cookies = await this.user.getAuthenticationCookies();
448
446
 
@@ -452,6 +450,10 @@ export class PrismicRepositoryManager extends BaseManager {
452
450
  extraHeaders["Content-Type"] = "application/json";
453
451
  }
454
452
 
453
+ if (args.repository) {
454
+ extraHeaders.repository = args.repository;
455
+ }
456
+
455
457
  return await fetch(args.url.toString(), {
456
458
  method: args.method,
457
459
  body: args.body ? JSON.stringify(args.body) : undefined,
@@ -148,4 +148,14 @@ export class SimulatorManager extends BaseManager {
148
148
  errors,
149
149
  };
150
150
  }
151
+
152
+ supportsSliceSimulator(): boolean {
153
+ assertPluginsInitialized(this.sliceMachinePluginRunner);
154
+
155
+ const hooks = this.sliceMachinePluginRunner.hooksForType(
156
+ "slice-simulator:setup:read",
157
+ );
158
+
159
+ return hooks.length > 0;
160
+ }
151
161
  }
@@ -123,7 +123,7 @@ export class TelemetryManager extends BaseManager {
123
123
  this._segmentClient.track(
124
124
  payload as Parameters<typeof this._segmentClient.track>[0],
125
125
  (maybeError?: Error) => {
126
- if (maybeError) {
126
+ if (maybeError && import.meta.env.DEV) {
127
127
  // TODO: Not sure how we want to deal with that
128
128
  console.warn(
129
129
  `An error occurred during Segment tracking`,
@@ -159,7 +159,7 @@ export class TelemetryManager extends BaseManager {
159
159
 
160
160
  // TODO: Make sure client fails gracefully when no internet connection
161
161
  this._segmentClient.identify(payload, (maybeError?: Error) => {
162
- if (maybeError) {
162
+ if (maybeError && import.meta.env.DEV) {
163
163
  // TODO: Not sure how we want to deal with that
164
164
  console.warn(`An error occurred during Segment identify`, maybeError);
165
165
  }
@@ -196,7 +196,7 @@ export class TelemetryManager extends BaseManager {
196
196
  this._segmentClient.group(
197
197
  payload as Parameters<typeof this._segmentClient.group>[0],
198
198
  (maybeError?: Error) => {
199
- if (maybeError) {
199
+ if (maybeError && import.meta.env.DEV) {
200
200
  // TODO: Not sure how we want to deal with that
201
201
  console.warn(`An error occurred during Segment group`, maybeError);
202
202
  }