@slicemachine/manager 0.17.6-beta.3 → 0.17.6-beta.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slicemachine/manager",
3
- "version": "0.17.6-beta.3",
3
+ "version": "0.17.6-beta.4",
4
4
  "description": "Manage all aspects of a Slice Machine project.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -69,7 +69,7 @@
69
69
  "@prismicio/mocks": "2.0.0",
70
70
  "@prismicio/types-internal": "^2.2.0",
71
71
  "@segment/analytics-node": "1.1.3",
72
- "@slicemachine/plugin-kit": "0.4.32-beta.3",
72
+ "@slicemachine/plugin-kit": "0.4.32-beta.4",
73
73
  "@wooorm/starry-night": "^1.6.0",
74
74
  "cookie": "^0.5.0",
75
75
  "cors": "^2.8.5",
@@ -30,6 +30,7 @@ export {
30
30
  isUnauthenticatedError,
31
31
  isUnauthorizedError,
32
32
  isUnexpectedDataError,
33
+ isInvalidActiveEnvironmentError,
33
34
  } from "../errors";
34
35
 
35
36
  export { DecodeError } from "../lib/DecodeError";
package/src/errors.ts CHANGED
@@ -1,30 +1,29 @@
1
1
  import { HookError } from "@slicemachine/plugin-kit";
2
2
 
3
3
  export class SliceMachineError extends Error {
4
- _sliceMachineError = true;
5
- name = "SliceMachineError";
4
+ name = "SMSliceMachineError";
6
5
  }
7
6
  export class UnauthorizedError extends SliceMachineError {
8
- name = "UnauthorizedError";
7
+ name = "SMUnauthorizedError" as const;
9
8
  }
10
9
  export class UnauthenticatedError extends SliceMachineError {
11
- name = "UnauthenticatedError";
10
+ name = "SMUnauthenticatedError" as const;
12
11
  message = "Authenticate before trying again.";
13
12
  }
14
13
  export class NotFoundError extends SliceMachineError {
15
- name = "NotFoundError";
14
+ name = "SMNotFoundError" as const;
16
15
  }
17
16
  export class UnexpectedDataError extends SliceMachineError {
18
- name = "UnexpectedDataError";
17
+ name = "SMUnexpectedDataError" as const;
19
18
  }
20
19
  export class InternalError extends SliceMachineError {
21
- name = "InternalError";
20
+ name = "SMInternalError" as const;
22
21
  }
23
22
  export class PluginError extends SliceMachineError {
24
- name = "PluginError";
23
+ name = "SMPluginError" as const;
25
24
  }
26
25
  export class PluginHookResultError extends SliceMachineError {
27
- name = "PluginHookResultError";
26
+ name = "SMPluginHookResultError" as const;
28
27
 
29
28
  constructor(errors: HookError[]) {
30
29
  super(
@@ -37,49 +36,74 @@ export class PluginHookResultError extends SliceMachineError {
37
36
  );
38
37
  }
39
38
  }
39
+ export class InvalidActiveEnvironmentError extends SliceMachineError {
40
+ name = "SMInvalidActiveEnvironmentError" as const;
41
+ }
42
+
43
+ type SliceMachineErrorNames =
44
+ | "SMSliceMachineError"
45
+ | UnauthorizedError["name"]
46
+ | UnauthenticatedError["name"]
47
+ | NotFoundError["name"]
48
+ | UnexpectedDataError["name"]
49
+ | InternalError["name"]
50
+ | PluginError["name"]
51
+ | PluginHookResultError["name"]
52
+ | InvalidActiveEnvironmentError["name"];
53
+
54
+ type ShallowSliceMachineError<TName extends SliceMachineErrorNames> = Error & {
55
+ name: TName;
56
+ };
40
57
 
41
- export const isSliceMachineError = (
58
+ export const isSliceMachineError = <TName extends SliceMachineErrorNames>(
42
59
  error: unknown,
43
- ): error is SliceMachineError => {
44
- // TODO: Discuss a stronger way to serialize error for the client to detect with r19
45
- // @ts-expect-error We don't want to add "dom" to tsconfig "lib" because of the TODO
46
- if (typeof window !== "undefined") {
47
- return typeof error === "object" && error !== null;
48
- } else {
49
- return (
50
- typeof error === "object" &&
51
- error !== null &&
52
- "_sliceMachineError" in error
53
- );
54
- }
60
+ name?: TName,
61
+ ): error is TName extends string ? ShallowSliceMachineError<TName> : Error => {
62
+ const isErrorInstance = error instanceof Error;
63
+
64
+ return name === undefined
65
+ ? isErrorInstance && error.name.startsWith("SM")
66
+ : isErrorInstance && error.name === name;
55
67
  };
56
68
 
57
69
  export const isUnauthorizedError = (
58
70
  error: unknown,
59
- ): error is UnauthorizedError => {
60
- return isSliceMachineError(error) && error.name === UnauthorizedError.name;
71
+ ): error is ShallowSliceMachineError<"SMUnauthorizedError"> => {
72
+ return isSliceMachineError(error, "SMUnauthorizedError");
61
73
  };
62
74
 
63
75
  export const isUnauthenticatedError = (
64
76
  error: unknown,
65
- ): error is UnauthenticatedError => {
66
- return isSliceMachineError(error) && error.name === UnauthenticatedError.name;
77
+ ): error is ShallowSliceMachineError<"SMUnauthenticatedError"> => {
78
+ return isSliceMachineError(error, "SMUnauthenticatedError");
67
79
  };
68
80
 
69
- export const isNotFoundError = (error: unknown): error is NotFoundError => {
70
- return isSliceMachineError(error) && error.name === NotFoundError.name;
81
+ export const isNotFoundError = (
82
+ error: unknown,
83
+ ): error is ShallowSliceMachineError<"SMNotFoundError"> => {
84
+ return isSliceMachineError(error, "SMNotFoundError");
71
85
  };
72
86
 
73
87
  export const isUnexpectedDataError = (
74
88
  error: unknown,
75
- ): error is UnexpectedDataError => {
76
- return isSliceMachineError(error) && error.name === UnexpectedDataError.name;
89
+ ): error is ShallowSliceMachineError<"SMUnexpectedDataError"> => {
90
+ return isSliceMachineError(error, "SMUnexpectedDataError");
91
+ };
92
+
93
+ export const isInternalError = (
94
+ error: unknown,
95
+ ): error is ShallowSliceMachineError<"SMInternalError"> => {
96
+ return isSliceMachineError(error, "SMInternalError");
77
97
  };
78
98
 
79
- export const isInternalError = (error: unknown): error is InternalError => {
80
- return isSliceMachineError(error) && error.name === InternalError.name;
99
+ export const isPluginError = (
100
+ error: unknown,
101
+ ): error is ShallowSliceMachineError<"SMPluginError"> => {
102
+ return isSliceMachineError(error, "SMPluginError");
81
103
  };
82
104
 
83
- export const isPluginError = (error: unknown): error is PluginError => {
84
- return isSliceMachineError(error) && error.name === PluginError.name;
105
+ export const isInvalidActiveEnvironmentError = (
106
+ error: unknown,
107
+ ): error is ShallowSliceMachineError<"SMInvalidActiveEnvironmentError"> => {
108
+ return isSliceMachineError(error, "SMInvalidActiveEnvironmentError");
85
109
  };
package/src/index.ts CHANGED
@@ -36,6 +36,7 @@ export {
36
36
  InternalError,
37
37
  PluginError,
38
38
  PluginHookResultError,
39
+ InvalidActiveEnvironmentError,
39
40
  } from "./errors";
40
41
 
41
42
  export { getEnvironmentInfo } from "./getEnvironmentInfo";
@@ -14,7 +14,6 @@ import {
14
14
  UnauthenticatedError,
15
15
  UnauthorizedError,
16
16
  UnexpectedDataError,
17
- isUnauthenticatedError,
18
17
  } from "../../errors";
19
18
 
20
19
  import { BaseManager } from "../BaseManager";
@@ -67,12 +66,19 @@ type PrismicRepositoryManagerPushDocumentsArgs = {
67
66
  };
68
67
 
69
68
  type PrismicRepositoryManagerFetchEnvironmentsArgs = {
70
- includeAllPersonalEnvironments?: boolean;
69
+ /**
70
+ * If set to `true`, all environments are returned regardless of the user's
71
+ * permission level.
72
+ *
73
+ * If set to `false`, only environments the user can access are returned.
74
+ *
75
+ * @defaultValue `false`
76
+ */
77
+ includeAll?: boolean;
71
78
  };
72
79
 
73
80
  type PrismicRepositoryManagerFetchEnvironmentsReturnType = {
74
81
  environments?: Environment[];
75
- error?: unknown;
76
82
  };
77
83
 
78
84
  export class PrismicRepositoryManager extends BaseManager {
@@ -451,20 +457,7 @@ export class PrismicRepositoryManager extends BaseManager {
451
457
  const url = new URL(`./environments`, API_ENDPOINTS.SliceMachineV1);
452
458
  url.searchParams.set("repository", repositoryName);
453
459
 
454
- let res;
455
- try {
456
- res = await this._fetch({ url });
457
- } catch (error) {
458
- if (isUnauthenticatedError(error)) {
459
- return { error };
460
- }
461
-
462
- return {
463
- error: new UnexpectedDataError(
464
- "Unexpected Error while fetching Environments",
465
- ),
466
- };
467
- }
460
+ const res = await this._fetch({ url });
468
461
 
469
462
  if (res.ok) {
470
463
  const json = await res.json();
@@ -482,44 +475,20 @@ export class PrismicRepositoryManager extends BaseManager {
482
475
  );
483
476
 
484
477
  if (error) {
485
- return {
486
- error: new UnexpectedDataError(
487
- `Failed to decode environments: ${error.errors.join(", ")}`,
488
- ),
489
- };
478
+ throw new UnexpectedDataError(
479
+ `Failed to decode environments: ${error.errors.join(", ")}`,
480
+ );
490
481
  }
491
482
 
492
483
  if ("results" in value) {
493
484
  let environments = value.results;
494
485
 
495
- // Only include the user's personal environment
496
- // by default. We must filter in the manager
497
- // because the API returns all personal
498
- // environments.
499
- if (!args?.includeAllPersonalEnvironments) {
500
- try {
501
- const profile = await this.user.getProfile();
502
-
503
- environments = environments.filter((environment) => {
504
- if (environment.kind === "dev") {
505
- return environment.users.some(
506
- (user) => user.id === profile.shortId,
507
- );
508
- }
509
-
510
- return true;
511
- });
512
- } catch (e) {
513
- if (isUnauthenticatedError(error)) {
514
- return { error };
515
- }
486
+ if (!args?.includeAll) {
487
+ const profile = await this.user.getProfile();
516
488
 
517
- return {
518
- error: new UnexpectedDataError(
519
- "Unexpected Error while fetching Environments",
520
- ),
521
- };
522
- }
489
+ environments = environments.filter((environment) =>
490
+ environment.users.some((user) => user.id === profile.shortId),
491
+ );
523
492
  }
524
493
 
525
494
  return { environments: sortEnvironments(environments) };
@@ -529,11 +498,11 @@ export class PrismicRepositoryManager extends BaseManager {
529
498
  switch (res.status) {
530
499
  case 400:
531
500
  case 401:
532
- return { error: new UnauthenticatedError() };
501
+ throw new UnauthenticatedError();
533
502
  case 403:
534
- return { error: new UnauthorizedError() };
503
+ throw new UnauthorizedError();
535
504
  default:
536
- return { error: new Error("Failed to fetch environments.") };
505
+ throw new Error("Failed to fetch environments.");
537
506
  }
538
507
  }
539
508
 
@@ -30,7 +30,7 @@ import {
30
30
  SliceMachineError,
31
31
  InternalError,
32
32
  PluginError,
33
- UnexpectedDataError,
33
+ InvalidActiveEnvironmentError,
34
34
  } from "../../errors";
35
35
 
36
36
  import { SLICE_MACHINE_CONFIG_FILENAME } from "../../constants/SLICE_MACHINE_CONFIG_FILENAME";
@@ -472,11 +472,7 @@ export class ProjectManager extends BaseManager {
472
472
  );
473
473
 
474
474
  if (!activeEnvironment) {
475
- throw new UnexpectedDataError(
476
- `The active environment (${
477
- activeEnvironmentDomain ?? "Production"
478
- }) does not match one of the repository's environments.`,
479
- );
475
+ throw new InvalidActiveEnvironmentError();
480
476
  }
481
477
 
482
478
  return { activeEnvironment };