@prismicio/plugin-kit 0.0.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/dist/_node_modules/lower-case/dist.es2015/index.js +7 -0
- package/dist/_node_modules/lower-case/dist.es2015/index.js.map +1 -0
- package/dist/_node_modules/no-case/dist.es2015/index.js +28 -0
- package/dist/_node_modules/no-case/dist.es2015/index.js.map +1 -0
- package/dist/_node_modules/p-limit/_node_modules/yocto-queue/index.js +68 -0
- package/dist/_node_modules/p-limit/_node_modules/yocto-queue/index.js.map +1 -0
- package/dist/_node_modules/p-limit/index.js +77 -0
- package/dist/_node_modules/p-limit/index.js.map +1 -0
- package/dist/_node_modules/pascal-case/dist.es2015/index.js +25 -0
- package/dist/_node_modules/pascal-case/dist.es2015/index.js.map +1 -0
- package/dist/_node_modules/tslib/tslib.es6.js +14 -0
- package/dist/_node_modules/tslib/tslib.es6.js.map +1 -0
- package/dist/createPluginHookSystem.d.ts +6 -0
- package/dist/createPluginHookSystem.js +8 -0
- package/dist/createPluginHookSystem.js.map +1 -0
- package/dist/createPluginSystemActions.d.ts +46 -0
- package/dist/createPluginSystemActions.js +96 -0
- package/dist/createPluginSystemActions.js.map +1 -0
- package/dist/createPluginSystemContext.d.ts +31 -0
- package/dist/createPluginSystemContext.js +12 -0
- package/dist/createPluginSystemContext.js.map +1 -0
- package/dist/createPluginSystemHelpers.d.ts +37 -0
- package/dist/createPluginSystemHelpers.js +72 -0
- package/dist/createPluginSystemHelpers.js.map +1 -0
- package/dist/createPluginSystemRunner.d.ts +58 -0
- package/dist/createPluginSystemRunner.js +128 -0
- package/dist/createPluginSystemRunner.js.map +1 -0
- package/dist/definePlugin.d.ts +30 -0
- package/dist/definePlugin.js +5 -0
- package/dist/definePlugin.js.map +1 -0
- package/dist/fs/buildCustomTypeDirectoryPath.d.ts +5 -0
- package/dist/fs/buildCustomTypeDirectoryPath.js +9 -0
- package/dist/fs/buildCustomTypeDirectoryPath.js.map +1 -0
- package/dist/fs/buildCustomTypeFilePath.d.ts +5 -0
- package/dist/fs/buildCustomTypeFilePath.js +9 -0
- package/dist/fs/buildCustomTypeFilePath.js.map +1 -0
- package/dist/fs/buildCustomTypeLibraryDirectoryPath.d.ts +6 -0
- package/dist/fs/buildCustomTypeLibraryDirectoryPath.js +8 -0
- package/dist/fs/buildCustomTypeLibraryDirectoryPath.js.map +1 -0
- package/dist/fs/buildSliceDirectoryPath.d.ts +4 -0
- package/dist/fs/buildSliceDirectoryPath.js +12 -0
- package/dist/fs/buildSliceDirectoryPath.js.map +1 -0
- package/dist/fs/buildSliceFilePath.d.ts +5 -0
- package/dist/fs/buildSliceFilePath.js +9 -0
- package/dist/fs/buildSliceFilePath.js.map +1 -0
- package/dist/fs/buildSliceLibraryDirectoryPath.d.ts +7 -0
- package/dist/fs/buildSliceLibraryDirectoryPath.js +8 -0
- package/dist/fs/buildSliceLibraryDirectoryPath.js.map +1 -0
- package/dist/fs/checkHasProjectFile.d.ts +6 -0
- package/dist/fs/checkHasProjectFile.js +9 -0
- package/dist/fs/checkHasProjectFile.js.map +1 -0
- package/dist/fs/checkIsTypeScriptProject.d.ts +4 -0
- package/dist/fs/checkIsTypeScriptProject.js +12 -0
- package/dist/fs/checkIsTypeScriptProject.js.map +1 -0
- package/dist/fs/constants.d.ts +9 -0
- package/dist/fs/constants.js +15 -0
- package/dist/fs/constants.js.map +1 -0
- package/dist/fs/deleteCustomTypeDirectory.d.ts +3 -0
- package/dist/fs/deleteCustomTypeDirectory.js +18 -0
- package/dist/fs/deleteCustomTypeDirectory.js.map +1 -0
- package/dist/fs/deleteProjectFile.d.ts +6 -0
- package/dist/fs/deleteProjectFile.js +10 -0
- package/dist/fs/deleteProjectFile.js.map +1 -0
- package/dist/fs/deleteSliceDirectory.d.ts +3 -0
- package/dist/fs/deleteSliceDirectory.js +18 -0
- package/dist/fs/deleteSliceDirectory.js.map +1 -0
- package/dist/fs/deleteSliceFile.d.ts +3 -0
- package/dist/fs/deleteSliceFile.js +18 -0
- package/dist/fs/deleteSliceFile.js.map +1 -0
- package/dist/fs/index.d.ts +49 -0
- package/dist/fs/index.js +53 -0
- package/dist/fs/index.js.map +1 -0
- package/dist/fs/lib/checkPathExists.d.ts +2 -0
- package/dist/fs/lib/checkPathExists.js +13 -0
- package/dist/fs/lib/checkPathExists.js.map +1 -0
- package/dist/fs/lib/fsLimit.d.ts +31 -0
- package/dist/fs/lib/fsLimit.js +23 -0
- package/dist/fs/lib/fsLimit.js.map +1 -0
- package/dist/fs/lib/isCustomTypeModel.d.ts +2 -0
- package/dist/fs/lib/isCustomTypeModel.js +7 -0
- package/dist/fs/lib/isCustomTypeModel.js.map +1 -0
- package/dist/fs/lib/isSharedSliceModel.d.ts +2 -0
- package/dist/fs/lib/isSharedSliceModel.js +7 -0
- package/dist/fs/lib/isSharedSliceModel.js.map +1 -0
- package/dist/fs/lib/pascalCase.d.ts +8 -0
- package/dist/fs/lib/pascalCase.js +10 -0
- package/dist/fs/lib/pascalCase.js.map +1 -0
- package/dist/fs/lib/readJSONFile.d.ts +1 -0
- package/dist/fs/lib/readJSONFile.js +9 -0
- package/dist/fs/lib/readJSONFile.js.map +1 -0
- package/dist/fs/lib/resolveSliceModel.d.ts +10 -0
- package/dist/fs/lib/resolveSliceModel.js +14 -0
- package/dist/fs/lib/resolveSliceModel.js.map +1 -0
- package/dist/fs/readCustomTypeFile.d.ts +8 -0
- package/dist/fs/readCustomTypeFile.js +14 -0
- package/dist/fs/readCustomTypeFile.js.map +1 -0
- package/dist/fs/readCustomTypeLibrary.d.ts +6 -0
- package/dist/fs/readCustomTypeLibrary.js +53 -0
- package/dist/fs/readCustomTypeLibrary.js.map +1 -0
- package/dist/fs/readCustomTypeModel.d.ts +7 -0
- package/dist/fs/readCustomTypeModel.js +16 -0
- package/dist/fs/readCustomTypeModel.js.map +1 -0
- package/dist/fs/readProjectFile.d.ts +12 -0
- package/dist/fs/readProjectFile.js +9 -0
- package/dist/fs/readProjectFile.js.map +1 -0
- package/dist/fs/readSliceFile.d.ts +8 -0
- package/dist/fs/readSliceFile.js +17 -0
- package/dist/fs/readSliceFile.js.map +1 -0
- package/dist/fs/readSliceLibrary.d.ts +7 -0
- package/dist/fs/readSliceLibrary.js +56 -0
- package/dist/fs/readSliceLibrary.js.map +1 -0
- package/dist/fs/readSliceModel.d.ts +11 -0
- package/dist/fs/readSliceModel.js +46 -0
- package/dist/fs/readSliceModel.js.map +1 -0
- package/dist/fs/renameCustomType.d.ts +3 -0
- package/dist/fs/renameCustomType.js +8 -0
- package/dist/fs/renameCustomType.js.map +1 -0
- package/dist/fs/renameSlice.d.ts +7 -0
- package/dist/fs/renameSlice.js +22 -0
- package/dist/fs/renameSlice.js.map +1 -0
- package/dist/fs/types.d.ts +3 -0
- package/dist/fs/upsertGlobalTypeScriptTypes.d.ts +16 -0
- package/dist/fs/upsertGlobalTypeScriptTypes.js +37 -0
- package/dist/fs/upsertGlobalTypeScriptTypes.js.map +1 -0
- package/dist/fs/writeCustomTypeFile.d.ts +9 -0
- package/dist/fs/writeCustomTypeFile.js +18 -0
- package/dist/fs/writeCustomTypeFile.js.map +1 -0
- package/dist/fs/writeCustomTypeModel.d.ts +12 -0
- package/dist/fs/writeCustomTypeModel.js +14 -0
- package/dist/fs/writeCustomTypeModel.js.map +1 -0
- package/dist/fs/writeProjectFile.d.ts +15 -0
- package/dist/fs/writeProjectFile.js +16 -0
- package/dist/fs/writeProjectFile.js.map +1 -0
- package/dist/fs/writeSliceFile.d.ts +9 -0
- package/dist/fs/writeSliceFile.js +18 -0
- package/dist/fs/writeSliceFile.js.map +1 -0
- package/dist/fs/writeSliceModel.d.ts +11 -0
- package/dist/fs/writeSliceModel.js +13 -0
- package/dist/fs/writeSliceModel.js.map +1 -0
- package/dist/hooks/customType-create.d.ts +28 -0
- package/dist/hooks/customType-delete.d.ts +26 -0
- package/dist/hooks/customType-read.d.ts +32 -0
- package/dist/hooks/customType-rename.d.ts +29 -0
- package/dist/hooks/customType-update.d.ts +29 -0
- package/dist/hooks/customTypeLibrary-read.d.ts +25 -0
- package/dist/hooks/debug.d.ts +22 -0
- package/dist/hooks/project-init.d.ts +35 -0
- package/dist/hooks/slice-create.d.ts +30 -0
- package/dist/hooks/slice-delete.d.ts +26 -0
- package/dist/hooks/slice-read.d.ts +32 -0
- package/dist/hooks/slice-rename.d.ts +26 -0
- package/dist/hooks/slice-update.d.ts +29 -0
- package/dist/hooks/sliceLibrary-read.d.ts +27 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/DecodeError.d.ts +13 -0
- package/dist/lib/DecodeError.js +21 -0
- package/dist/lib/DecodeError.js.map +1 -0
- package/dist/lib/HookSystem.d.ts +93 -0
- package/dist/lib/HookSystem.js +121 -0
- package/dist/lib/HookSystem.js.map +1 -0
- package/dist/lib/decode.d.ts +10 -0
- package/dist/lib/decode.js +16 -0
- package/dist/lib/decode.js.map +1 -0
- package/dist/lib/decodePrismicConfig.d.ts +3 -0
- package/dist/lib/decodePrismicConfig.js +22 -0
- package/dist/lib/decodePrismicConfig.js.map +1 -0
- package/dist/types.d.ts +129 -0
- package/dist/types.js +21 -0
- package/dist/types.js.map +1 -0
- package/package.json +90 -0
- package/src/createPluginHookSystem.ts +9 -0
- package/src/createPluginSystemActions.ts +188 -0
- package/src/createPluginSystemContext.ts +51 -0
- package/src/createPluginSystemHelpers.ts +129 -0
- package/src/createPluginSystemRunner.ts +216 -0
- package/src/definePlugin.ts +48 -0
- package/src/fs/buildCustomTypeDirectoryPath.ts +19 -0
- package/src/fs/buildCustomTypeFilePath.ts +16 -0
- package/src/fs/buildCustomTypeLibraryDirectoryPath.ts +16 -0
- package/src/fs/buildSliceDirectoryPath.ts +25 -0
- package/src/fs/buildSliceFilePath.ts +16 -0
- package/src/fs/buildSliceLibraryDirectoryPath.ts +17 -0
- package/src/fs/checkHasProjectFile.ts +16 -0
- package/src/fs/checkIsTypeScriptProject.ts +16 -0
- package/src/fs/constants.ts +15 -0
- package/src/fs/deleteCustomTypeDirectory.ts +24 -0
- package/src/fs/deleteProjectFile.ts +18 -0
- package/src/fs/deleteSliceDirectory.ts +24 -0
- package/src/fs/deleteSliceFile.ts +24 -0
- package/src/fs/index.ts +103 -0
- package/src/fs/lib/checkPathExists.ts +13 -0
- package/src/fs/lib/fsLimit.ts +72 -0
- package/src/fs/lib/isCustomTypeModel.ts +7 -0
- package/src/fs/lib/isSharedSliceModel.ts +14 -0
- package/src/fs/lib/pascalCase.ts +17 -0
- package/src/fs/lib/readJSONFile.ts +7 -0
- package/src/fs/lib/resolveSliceModel.ts +28 -0
- package/src/fs/readCustomTypeFile.ts +27 -0
- package/src/fs/readCustomTypeLibrary.ts +86 -0
- package/src/fs/readCustomTypeModel.ts +27 -0
- package/src/fs/readProjectFile.ts +24 -0
- package/src/fs/readSliceFile.ts +27 -0
- package/src/fs/readSliceLibrary.ts +91 -0
- package/src/fs/readSliceModel.ts +90 -0
- package/src/fs/renameCustomType.ts +12 -0
- package/src/fs/renameSlice.ts +35 -0
- package/src/fs/types.ts +5 -0
- package/src/fs/upsertGlobalTypeScriptTypes.ts +64 -0
- package/src/fs/writeCustomTypeFile.ts +30 -0
- package/src/fs/writeCustomTypeModel.ts +31 -0
- package/src/fs/writeProjectFile.ts +39 -0
- package/src/fs/writeSliceFile.ts +30 -0
- package/src/fs/writeSliceModel.ts +26 -0
- package/src/hooks/customType-create.ts +42 -0
- package/src/hooks/customType-delete.ts +40 -0
- package/src/hooks/customType-read.ts +46 -0
- package/src/hooks/customType-rename.ts +43 -0
- package/src/hooks/customType-update.ts +43 -0
- package/src/hooks/customTypeLibrary-read.ts +38 -0
- package/src/hooks/debug.ts +31 -0
- package/src/hooks/project-init.ts +48 -0
- package/src/hooks/slice-create.ts +44 -0
- package/src/hooks/slice-delete.ts +40 -0
- package/src/hooks/slice-read.ts +44 -0
- package/src/hooks/slice-rename.ts +40 -0
- package/src/hooks/slice-update.ts +43 -0
- package/src/hooks/sliceLibrary-read.ts +41 -0
- package/src/index.ts +141 -0
- package/src/lib/DecodeError.ts +31 -0
- package/src/lib/HookSystem.ts +277 -0
- package/src/lib/decode.ts +30 -0
- package/src/lib/decodePrismicConfig.ts +26 -0
- package/src/types.ts +175 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtendPluginSystemHook,
|
|
3
|
+
PluginOptions,
|
|
4
|
+
SliceLibrary,
|
|
5
|
+
PluginHook,
|
|
6
|
+
} from "../types";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Data provided to `slice-library:read` hook handlers.
|
|
10
|
+
*/
|
|
11
|
+
export type SliceLibraryReadHookData = {
|
|
12
|
+
libraryID: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Return value for `slice-library:read` hook handlers.
|
|
17
|
+
*/
|
|
18
|
+
export type SliceLibraryReadHookReturnType = SliceLibrary & {
|
|
19
|
+
sliceIDs: string[];
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Base version of a `slice-library:read` hook handler without plugin runner
|
|
24
|
+
* context.
|
|
25
|
+
*
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
export type SliceLibraryReadHookBase = PluginHook<
|
|
29
|
+
SliceLibraryReadHookData,
|
|
30
|
+
SliceLibraryReadHookReturnType
|
|
31
|
+
>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Handler for the `slice-library:read` hook. The hook is called when a Slice
|
|
35
|
+
* Library's metadata is needed.
|
|
36
|
+
*
|
|
37
|
+
* @typeParam TPluginOptions - User-provided options for the hook's plugin.
|
|
38
|
+
*/
|
|
39
|
+
export type SliceLibraryReadHook<
|
|
40
|
+
TPluginOptions extends PluginOptions = PluginOptions,
|
|
41
|
+
> = ExtendPluginSystemHook<SliceLibraryReadHookBase, TPluginOptions>;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
//
|
|
3
|
+
// Public (for plugin authors)
|
|
4
|
+
//
|
|
5
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6
|
+
|
|
7
|
+
export { definePlugin } from "./definePlugin";
|
|
8
|
+
export type { Plugin } from "./definePlugin";
|
|
9
|
+
|
|
10
|
+
export type {
|
|
11
|
+
PluginSystemActions,
|
|
12
|
+
ReadAllSliceModelsActionArgs,
|
|
13
|
+
ReadAllSliceModelsActionReturnType,
|
|
14
|
+
ReadAllSliceModelsForLibraryActionArgs,
|
|
15
|
+
} from "./createPluginSystemActions";
|
|
16
|
+
export type { PluginSystemHelpers } from "./createPluginSystemHelpers";
|
|
17
|
+
export type { PluginSystemContext } from "./createPluginSystemContext";
|
|
18
|
+
|
|
19
|
+
export { PluginHookType } from "./types";
|
|
20
|
+
export type {
|
|
21
|
+
PluginOptions,
|
|
22
|
+
PrismicProject,
|
|
23
|
+
PrismicConfig,
|
|
24
|
+
SliceLibrary,
|
|
25
|
+
PluginHooks,
|
|
26
|
+
PluginHookTypes,
|
|
27
|
+
} from "./types";
|
|
28
|
+
|
|
29
|
+
export { HookError } from "./lib/HookSystem";
|
|
30
|
+
export { DecodeError } from "./lib/DecodeError";
|
|
31
|
+
|
|
32
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
33
|
+
//
|
|
34
|
+
// Hooks (for plugin authors)
|
|
35
|
+
//
|
|
36
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
37
|
+
|
|
38
|
+
// slice:create
|
|
39
|
+
export type {
|
|
40
|
+
SliceCreateHook,
|
|
41
|
+
SliceCreateHookData,
|
|
42
|
+
SliceCreateHookReturnType,
|
|
43
|
+
} from "./hooks/slice-create";
|
|
44
|
+
|
|
45
|
+
// slice:update
|
|
46
|
+
export type {
|
|
47
|
+
SliceUpdateHook,
|
|
48
|
+
SliceUpdateHookData,
|
|
49
|
+
SliceUpdateHookReturnType,
|
|
50
|
+
} from "./hooks/slice-update";
|
|
51
|
+
|
|
52
|
+
// slice:rename
|
|
53
|
+
export type {
|
|
54
|
+
SliceRenameHook,
|
|
55
|
+
SliceRenameHookData,
|
|
56
|
+
SliceRenameHookReturnType,
|
|
57
|
+
} from "./hooks/slice-rename";
|
|
58
|
+
|
|
59
|
+
// slice:delete
|
|
60
|
+
export type {
|
|
61
|
+
SliceDeleteHook,
|
|
62
|
+
SliceDeleteHookData,
|
|
63
|
+
SliceDeleteHookReturnType,
|
|
64
|
+
} from "./hooks/slice-delete";
|
|
65
|
+
|
|
66
|
+
// slice:read
|
|
67
|
+
export type {
|
|
68
|
+
SliceReadHook,
|
|
69
|
+
SliceReadHookData,
|
|
70
|
+
SliceReadHookReturnType,
|
|
71
|
+
} from "./hooks/slice-read";
|
|
72
|
+
|
|
73
|
+
// slice-library:read
|
|
74
|
+
export type {
|
|
75
|
+
SliceLibraryReadHook,
|
|
76
|
+
SliceLibraryReadHookData,
|
|
77
|
+
SliceLibraryReadHookReturnType,
|
|
78
|
+
} from "./hooks/sliceLibrary-read";
|
|
79
|
+
|
|
80
|
+
// custom-type:create
|
|
81
|
+
export type {
|
|
82
|
+
CustomTypeCreateHook,
|
|
83
|
+
CustomTypeCreateHookData,
|
|
84
|
+
CustomTypeCreateHookReturnType,
|
|
85
|
+
} from "./hooks/customType-create";
|
|
86
|
+
|
|
87
|
+
// custom-type:update
|
|
88
|
+
export type {
|
|
89
|
+
CustomTypeUpdateHook,
|
|
90
|
+
CustomTypeUpdateHookData,
|
|
91
|
+
CustomTypeUpdateHookReturnType,
|
|
92
|
+
} from "./hooks/customType-update";
|
|
93
|
+
|
|
94
|
+
// custom-type:rename
|
|
95
|
+
export type {
|
|
96
|
+
CustomTypeRenameHook,
|
|
97
|
+
CustomTypeRenameHookData,
|
|
98
|
+
CustomTypeRenameHookReturnType,
|
|
99
|
+
} from "./hooks/customType-rename";
|
|
100
|
+
|
|
101
|
+
// custom-type:delete
|
|
102
|
+
export type {
|
|
103
|
+
CustomTypeDeleteHook,
|
|
104
|
+
CustomTypeDeleteHookData,
|
|
105
|
+
CustomTypeDeleteHookReturnType,
|
|
106
|
+
} from "./hooks/customType-delete";
|
|
107
|
+
|
|
108
|
+
// custom-type:read
|
|
109
|
+
export type {
|
|
110
|
+
CustomTypeReadHook,
|
|
111
|
+
CustomTypeReadHookData,
|
|
112
|
+
CustomTypeReadHookReturnType,
|
|
113
|
+
} from "./hooks/customType-read";
|
|
114
|
+
|
|
115
|
+
// custom-type-library:read
|
|
116
|
+
export type {
|
|
117
|
+
CustomTypeLibraryReadHook,
|
|
118
|
+
CustomTypeLibraryReadHookData,
|
|
119
|
+
CustomTypeLibraryReadHookReturnType,
|
|
120
|
+
} from "./hooks/customTypeLibrary-read";
|
|
121
|
+
|
|
122
|
+
// project:init
|
|
123
|
+
export type {
|
|
124
|
+
ProjectInitHook,
|
|
125
|
+
ProjectInitHookData,
|
|
126
|
+
ProjectInitHookReturnType,
|
|
127
|
+
} from "./hooks/project-init";
|
|
128
|
+
|
|
129
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
130
|
+
//
|
|
131
|
+
// Internal (for Plugin System)
|
|
132
|
+
//
|
|
133
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
134
|
+
|
|
135
|
+
export {
|
|
136
|
+
createPluginSystemRunner,
|
|
137
|
+
PluginSystemRunner,
|
|
138
|
+
REQUIRED_ADAPTER_HOOKS,
|
|
139
|
+
} from "./createPluginSystemRunner";
|
|
140
|
+
|
|
141
|
+
export type { CallHookReturnType } from "./lib/HookSystem";
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
|
|
3
|
+
type ZodIssue = z.ZodIssue;
|
|
4
|
+
|
|
5
|
+
type DecodeErrorConstructorArgs<TInput = unknown> = {
|
|
6
|
+
input: TInput;
|
|
7
|
+
errors: ZodIssue[];
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
function formatZodErrors(errors: ZodIssue[]): string[] {
|
|
11
|
+
return errors.map((err) => {
|
|
12
|
+
const path = err.path.length > 0 ? ` at ${err.path.join(".")}` : "";
|
|
13
|
+
|
|
14
|
+
return `${err.message}${path}`;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class DecodeError<TInput = unknown> extends Error {
|
|
19
|
+
name = "DecodeError";
|
|
20
|
+
input: TInput;
|
|
21
|
+
errors: string[];
|
|
22
|
+
|
|
23
|
+
constructor(args: DecodeErrorConstructorArgs<TInput>) {
|
|
24
|
+
const formattedErrors = formatZodErrors(args.errors);
|
|
25
|
+
|
|
26
|
+
super(formattedErrors.join(", "));
|
|
27
|
+
|
|
28
|
+
this.input = args.input;
|
|
29
|
+
this.errors = formattedErrors;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extends a function arguments with extra ones.
|
|
3
|
+
*/
|
|
4
|
+
type FnWithExtraArgs<
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
|
+
F extends (...args: any[]) => any,
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
|
+
TExtraArgs extends any[] = any[],
|
|
9
|
+
> = (
|
|
10
|
+
...args: [...args: Parameters<F>, ...extraArgs: TExtraArgs]
|
|
11
|
+
) => ReturnType<F>;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Defines a hook handler.
|
|
15
|
+
*/
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
17
|
+
export type HookFn<TArgs extends any[] = any[], TReturn = any> = (
|
|
18
|
+
...args: TArgs
|
|
19
|
+
) => Promise<TReturn> | TReturn;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Generic hook metadata.
|
|
23
|
+
*/
|
|
24
|
+
type HookMeta = Record<string, unknown>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Defines a hook, including its function handler and optional metadata.
|
|
28
|
+
*/
|
|
29
|
+
export type Hook<
|
|
30
|
+
THookFn extends HookFn = HookFn,
|
|
31
|
+
THookMeta extends HookMeta = HookMeta,
|
|
32
|
+
> = {
|
|
33
|
+
fn: THookFn;
|
|
34
|
+
meta?: THookMeta;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Represents a map of hook types to hook functions and metas.
|
|
39
|
+
*/
|
|
40
|
+
type Hooks = Record<string, Hook>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Builds hook meta arguments after hook meta requirements.
|
|
44
|
+
*/
|
|
45
|
+
type HookMetaArg<THookMeta extends Record<string, unknown> | undefined> =
|
|
46
|
+
THookMeta extends Record<string, unknown>
|
|
47
|
+
? [meta: THookMeta]
|
|
48
|
+
: [meta?: never];
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Defines the return type of the {@link HookSystem.callHook} functions.
|
|
52
|
+
*
|
|
53
|
+
* @internal
|
|
54
|
+
*/
|
|
55
|
+
export type CallHookReturnType<THookFn extends HookFn = HookFn> = Promise<{
|
|
56
|
+
data: Awaited<ReturnType<THookFn>>[];
|
|
57
|
+
errors: HookError[];
|
|
58
|
+
}>;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Defines the return type of the {@link HookSystem.createScope} functions.
|
|
62
|
+
*
|
|
63
|
+
* @internal
|
|
64
|
+
*/
|
|
65
|
+
export type CreateScopeReturnType<
|
|
66
|
+
THooks extends Hooks = Record<string, { fn: HookFn }>,
|
|
67
|
+
TExtraArgs extends unknown[] = never[],
|
|
68
|
+
> = {
|
|
69
|
+
hook: <TType extends keyof THooks>(
|
|
70
|
+
type: TType,
|
|
71
|
+
hookFn: FnWithExtraArgs<THooks[TType]["fn"], TExtraArgs>,
|
|
72
|
+
...[meta]: HookMetaArg<THooks[TType]["meta"]>
|
|
73
|
+
) => void;
|
|
74
|
+
unhook: HookSystem<{
|
|
75
|
+
[P in keyof THooks]: Omit<THooks[P], "fn"> & {
|
|
76
|
+
fn: FnWithExtraArgs<THooks[P]["fn"], TExtraArgs>;
|
|
77
|
+
};
|
|
78
|
+
}>["unhook"];
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
type RegisteredHookMeta = {
|
|
82
|
+
id: string;
|
|
83
|
+
type: string;
|
|
84
|
+
owner: string;
|
|
85
|
+
external?: HookFn;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Represents a registered hook.
|
|
90
|
+
*/
|
|
91
|
+
type RegisteredHook<THook extends Hook = Hook> = {
|
|
92
|
+
fn: THook["fn"];
|
|
93
|
+
meta: THook["meta"] extends Record<string, unknown>
|
|
94
|
+
? RegisteredHookMeta & THook["meta"]
|
|
95
|
+
: RegisteredHookMeta;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export class HookError<TError = Error | unknown> extends Error {
|
|
99
|
+
type: string;
|
|
100
|
+
owner: string;
|
|
101
|
+
rawMeta: RegisteredHookMeta;
|
|
102
|
+
rawCause: TError;
|
|
103
|
+
|
|
104
|
+
constructor(meta: RegisteredHookMeta, cause: TError) {
|
|
105
|
+
super(
|
|
106
|
+
`Error in \`${meta.owner}\` during \`${meta.type}\` hook: ${
|
|
107
|
+
cause instanceof Error ? cause.message : String(cause)
|
|
108
|
+
}`,
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
this.type = meta.type;
|
|
112
|
+
this.owner = meta.owner;
|
|
113
|
+
this.rawMeta = meta;
|
|
114
|
+
this.rawCause = cause;
|
|
115
|
+
this.cause = cause instanceof Error ? cause : undefined;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const uuid = (): string => {
|
|
120
|
+
return (++uuid.i).toString();
|
|
121
|
+
};
|
|
122
|
+
uuid.i = 0;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @internal
|
|
126
|
+
*/
|
|
127
|
+
export class HookSystem<THooks extends Hooks = Hooks> {
|
|
128
|
+
private _registeredHooks: {
|
|
129
|
+
[K in keyof THooks]?: RegisteredHook<THooks[K]>[];
|
|
130
|
+
} = {};
|
|
131
|
+
|
|
132
|
+
hook<TType extends keyof THooks>(
|
|
133
|
+
owner: string,
|
|
134
|
+
type: TType,
|
|
135
|
+
hookFn: THooks[TType]["fn"],
|
|
136
|
+
...[meta]: HookMetaArg<THooks[TType]["meta"]>
|
|
137
|
+
): void {
|
|
138
|
+
const registeredHook = {
|
|
139
|
+
fn: hookFn,
|
|
140
|
+
meta: {
|
|
141
|
+
...meta,
|
|
142
|
+
owner,
|
|
143
|
+
type,
|
|
144
|
+
id: uuid(),
|
|
145
|
+
},
|
|
146
|
+
} as RegisteredHook<THooks[TType]>;
|
|
147
|
+
|
|
148
|
+
const registeredHooksForType = this._registeredHooks[type];
|
|
149
|
+
|
|
150
|
+
if (registeredHooksForType) {
|
|
151
|
+
registeredHooksForType.push(registeredHook);
|
|
152
|
+
} else {
|
|
153
|
+
this._registeredHooks[type] = [registeredHook];
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
unhook<TType extends keyof THooks>(
|
|
158
|
+
type: TType,
|
|
159
|
+
hookFn: THooks[TType]["fn"],
|
|
160
|
+
): void {
|
|
161
|
+
this._registeredHooks[type] = this._registeredHooks[type]?.filter(
|
|
162
|
+
(registeredHook) => registeredHook.fn !== hookFn,
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async callHook<TType extends Extract<keyof THooks, string>>(
|
|
167
|
+
typeOrTypeAndHookID: TType | { type: TType; hookID: string },
|
|
168
|
+
...args: Parameters<THooks[TType]["fn"]>
|
|
169
|
+
): CallHookReturnType<THooks[TType]["fn"]> {
|
|
170
|
+
let hooks: RegisteredHook<THooks[TType]>[];
|
|
171
|
+
|
|
172
|
+
if (typeof typeOrTypeAndHookID === "string") {
|
|
173
|
+
hooks = this._registeredHooks[typeOrTypeAndHookID] ?? [];
|
|
174
|
+
} else {
|
|
175
|
+
const hookForID = this._registeredHooks[typeOrTypeAndHookID.type]?.find(
|
|
176
|
+
(hook) => hook.meta.id === typeOrTypeAndHookID.hookID,
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
if (hookForID) {
|
|
180
|
+
hooks = [hookForID];
|
|
181
|
+
} else {
|
|
182
|
+
throw new Error(
|
|
183
|
+
`Hook of type \`${typeOrTypeAndHookID.type}\` with ID \`${typeOrTypeAndHookID.hookID}\` not found.`,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const promises = hooks.map(async (hook) => {
|
|
189
|
+
try {
|
|
190
|
+
return await hook.fn(...args);
|
|
191
|
+
} catch (error) {
|
|
192
|
+
throw new HookError(hook.meta, error);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const settledPromises = await Promise.allSettled(promises);
|
|
197
|
+
|
|
198
|
+
return settledPromises.reduce<{
|
|
199
|
+
data: Awaited<ReturnType<THooks[TType]["fn"]>>[];
|
|
200
|
+
errors: HookError[];
|
|
201
|
+
}>(
|
|
202
|
+
(acc, settledPromise) => {
|
|
203
|
+
if (settledPromise.status === "fulfilled") {
|
|
204
|
+
acc.data.push(settledPromise.value);
|
|
205
|
+
} else {
|
|
206
|
+
acc.errors.push(settledPromise.reason);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return acc;
|
|
210
|
+
},
|
|
211
|
+
{ data: [], errors: [] },
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Returns list of hooks for a given owner
|
|
217
|
+
*/
|
|
218
|
+
hooksForOwner(owner: string): RegisteredHook<THooks[string]>[] {
|
|
219
|
+
const hooks: RegisteredHook<THooks[string]>[] = [];
|
|
220
|
+
|
|
221
|
+
for (const hookType in this._registeredHooks) {
|
|
222
|
+
const registeredHooks = this._registeredHooks[hookType];
|
|
223
|
+
|
|
224
|
+
if (Array.isArray(registeredHooks)) {
|
|
225
|
+
for (const registeredHook of registeredHooks) {
|
|
226
|
+
if (registeredHook.meta.owner === owner) {
|
|
227
|
+
hooks.push(registeredHook);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return hooks;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Returns list of hooks for a given type
|
|
238
|
+
*/
|
|
239
|
+
hooksForType<TType extends keyof THooks>(
|
|
240
|
+
type: TType,
|
|
241
|
+
): RegisteredHook<THooks[TType]>[] {
|
|
242
|
+
return this._registeredHooks[type] ?? [];
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
createScope<TExtraArgs extends unknown[] = never[]>(
|
|
246
|
+
owner: string,
|
|
247
|
+
extraArgs: [...TExtraArgs],
|
|
248
|
+
): CreateScopeReturnType<THooks, TExtraArgs> {
|
|
249
|
+
return {
|
|
250
|
+
hook: (type, hookFn, ...[meta]) => {
|
|
251
|
+
const internalHook = ((
|
|
252
|
+
...args: Parameters<THooks[typeof type]["fn"]>
|
|
253
|
+
) => {
|
|
254
|
+
return hookFn(...args, ...extraArgs);
|
|
255
|
+
}) as THooks[typeof type]["fn"];
|
|
256
|
+
|
|
257
|
+
const resolvedMeta = {
|
|
258
|
+
...meta,
|
|
259
|
+
external: hookFn,
|
|
260
|
+
} as HookMetaArg<THooks[typeof type]["meta"]>[0];
|
|
261
|
+
|
|
262
|
+
return this.hook(
|
|
263
|
+
owner,
|
|
264
|
+
type,
|
|
265
|
+
internalHook,
|
|
266
|
+
// @ts-expect-error - TypeScript fails to assert rest argument.
|
|
267
|
+
resolvedMeta,
|
|
268
|
+
);
|
|
269
|
+
},
|
|
270
|
+
unhook: (type, hookFn) => {
|
|
271
|
+
this._registeredHooks[type] = this._registeredHooks[type]?.filter(
|
|
272
|
+
(registeredHook) => registeredHook.meta.external !== hookFn,
|
|
273
|
+
);
|
|
274
|
+
},
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
|
|
3
|
+
import { DecodeError } from "./DecodeError";
|
|
4
|
+
|
|
5
|
+
export type DecodeReturnType<A, _O, I> =
|
|
6
|
+
| {
|
|
7
|
+
value: A;
|
|
8
|
+
error?: never;
|
|
9
|
+
}
|
|
10
|
+
| {
|
|
11
|
+
value?: never;
|
|
12
|
+
error: DecodeError<I>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const decode = <A, O, I>(
|
|
16
|
+
codec: z.ZodType<A>,
|
|
17
|
+
input: I,
|
|
18
|
+
): DecodeReturnType<A, O, I> => {
|
|
19
|
+
const parsed = codec.safeParse(input);
|
|
20
|
+
|
|
21
|
+
if (parsed.success) {
|
|
22
|
+
return {
|
|
23
|
+
value: parsed.data,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
error: new DecodeError({ input, errors: parsed.error.issues }),
|
|
29
|
+
};
|
|
30
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
|
|
3
|
+
import { PrismicConfig } from "../types";
|
|
4
|
+
|
|
5
|
+
import { decode, DecodeReturnType } from "./decode";
|
|
6
|
+
|
|
7
|
+
const PrismicConfigPluginRegistrationSchema = z.union([
|
|
8
|
+
z.string(),
|
|
9
|
+
z.object({
|
|
10
|
+
resolve: z.string(),
|
|
11
|
+
options: z.record(z.string(), z.unknown()).optional(),
|
|
12
|
+
}),
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
const PrismicConfigSchema = z.object({
|
|
16
|
+
repositoryName: z.string(),
|
|
17
|
+
adapter: PrismicConfigPluginRegistrationSchema,
|
|
18
|
+
apiEndpoint: z.string().optional(),
|
|
19
|
+
libraries: z.array(z.string()).optional(),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
export const decodePrismicConfig = (
|
|
23
|
+
input: unknown,
|
|
24
|
+
): DecodeReturnType<PrismicConfig, PrismicConfig, unknown> => {
|
|
25
|
+
return decode(PrismicConfigSchema, input);
|
|
26
|
+
};
|