@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/dist/client/index.d.ts +1 -1
- package/dist/client.cjs +1 -0
- package/dist/client.cjs.map +1 -1
- package/dist/client.js +2 -1
- package/dist/errors.cjs +27 -20
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.ts +23 -15
- package/dist/errors.js +27 -20
- package/dist/errors.js.map +1 -1
- package/dist/index.cjs +1 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -1
- package/dist/managers/prismicRepository/PrismicRepositoryManager.cjs +8 -34
- package/dist/managers/prismicRepository/PrismicRepositoryManager.cjs.map +1 -1
- package/dist/managers/prismicRepository/PrismicRepositoryManager.d.ts +9 -2
- package/dist/managers/prismicRepository/PrismicRepositoryManager.js +9 -35
- package/dist/managers/prismicRepository/PrismicRepositoryManager.js.map +1 -1
- package/dist/managers/project/ProjectManager.cjs +1 -1
- package/dist/managers/project/ProjectManager.cjs.map +1 -1
- package/dist/managers/project/ProjectManager.js +2 -2
- package/dist/managers/project/ProjectManager.js.map +1 -1
- package/package.json +2 -2
- package/src/client/index.ts +1 -0
- package/src/errors.ts +58 -34
- package/src/index.ts +1 -0
- package/src/managers/prismicRepository/PrismicRepositoryManager.ts +21 -52
- package/src/managers/project/ProjectManager.ts +2 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@slicemachine/manager",
|
|
3
|
-
"version": "0.17.6-beta.
|
|
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.
|
|
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",
|
package/src/client/index.ts
CHANGED
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
|
-
|
|
5
|
-
name = "SliceMachineError";
|
|
4
|
+
name = "SMSliceMachineError";
|
|
6
5
|
}
|
|
7
6
|
export class UnauthorizedError extends SliceMachineError {
|
|
8
|
-
name = "
|
|
7
|
+
name = "SMUnauthorizedError" as const;
|
|
9
8
|
}
|
|
10
9
|
export class UnauthenticatedError extends SliceMachineError {
|
|
11
|
-
name = "
|
|
10
|
+
name = "SMUnauthenticatedError" as const;
|
|
12
11
|
message = "Authenticate before trying again.";
|
|
13
12
|
}
|
|
14
13
|
export class NotFoundError extends SliceMachineError {
|
|
15
|
-
name = "
|
|
14
|
+
name = "SMNotFoundError" as const;
|
|
16
15
|
}
|
|
17
16
|
export class UnexpectedDataError extends SliceMachineError {
|
|
18
|
-
name = "
|
|
17
|
+
name = "SMUnexpectedDataError" as const;
|
|
19
18
|
}
|
|
20
19
|
export class InternalError extends SliceMachineError {
|
|
21
|
-
name = "
|
|
20
|
+
name = "SMInternalError" as const;
|
|
22
21
|
}
|
|
23
22
|
export class PluginError extends SliceMachineError {
|
|
24
|
-
name = "
|
|
23
|
+
name = "SMPluginError" as const;
|
|
25
24
|
}
|
|
26
25
|
export class PluginHookResultError extends SliceMachineError {
|
|
27
|
-
name = "
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
|
60
|
-
return isSliceMachineError(error)
|
|
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
|
|
66
|
-
return isSliceMachineError(error)
|
|
77
|
+
): error is ShallowSliceMachineError<"SMUnauthenticatedError"> => {
|
|
78
|
+
return isSliceMachineError(error, "SMUnauthenticatedError");
|
|
67
79
|
};
|
|
68
80
|
|
|
69
|
-
export const isNotFoundError = (
|
|
70
|
-
|
|
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
|
|
76
|
-
return isSliceMachineError(error)
|
|
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
|
|
80
|
-
|
|
99
|
+
export const isPluginError = (
|
|
100
|
+
error: unknown,
|
|
101
|
+
): error is ShallowSliceMachineError<"SMPluginError"> => {
|
|
102
|
+
return isSliceMachineError(error, "SMPluginError");
|
|
81
103
|
};
|
|
82
104
|
|
|
83
|
-
export const
|
|
84
|
-
|
|
105
|
+
export const isInvalidActiveEnvironmentError = (
|
|
106
|
+
error: unknown,
|
|
107
|
+
): error is ShallowSliceMachineError<"SMInvalidActiveEnvironmentError"> => {
|
|
108
|
+
return isSliceMachineError(error, "SMInvalidActiveEnvironmentError");
|
|
85
109
|
};
|
package/src/index.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
486
|
-
|
|
487
|
-
|
|
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
|
-
|
|
496
|
-
|
|
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
|
-
|
|
518
|
-
|
|
519
|
-
|
|
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
|
-
|
|
501
|
+
throw new UnauthenticatedError();
|
|
533
502
|
case 403:
|
|
534
|
-
|
|
503
|
+
throw new UnauthorizedError();
|
|
535
504
|
default:
|
|
536
|
-
|
|
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
|
-
|
|
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
|
|
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 };
|