@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.
Files changed (235) hide show
  1. package/dist/_node_modules/lower-case/dist.es2015/index.js +7 -0
  2. package/dist/_node_modules/lower-case/dist.es2015/index.js.map +1 -0
  3. package/dist/_node_modules/no-case/dist.es2015/index.js +28 -0
  4. package/dist/_node_modules/no-case/dist.es2015/index.js.map +1 -0
  5. package/dist/_node_modules/p-limit/_node_modules/yocto-queue/index.js +68 -0
  6. package/dist/_node_modules/p-limit/_node_modules/yocto-queue/index.js.map +1 -0
  7. package/dist/_node_modules/p-limit/index.js +77 -0
  8. package/dist/_node_modules/p-limit/index.js.map +1 -0
  9. package/dist/_node_modules/pascal-case/dist.es2015/index.js +25 -0
  10. package/dist/_node_modules/pascal-case/dist.es2015/index.js.map +1 -0
  11. package/dist/_node_modules/tslib/tslib.es6.js +14 -0
  12. package/dist/_node_modules/tslib/tslib.es6.js.map +1 -0
  13. package/dist/createPluginHookSystem.d.ts +6 -0
  14. package/dist/createPluginHookSystem.js +8 -0
  15. package/dist/createPluginHookSystem.js.map +1 -0
  16. package/dist/createPluginSystemActions.d.ts +46 -0
  17. package/dist/createPluginSystemActions.js +96 -0
  18. package/dist/createPluginSystemActions.js.map +1 -0
  19. package/dist/createPluginSystemContext.d.ts +31 -0
  20. package/dist/createPluginSystemContext.js +12 -0
  21. package/dist/createPluginSystemContext.js.map +1 -0
  22. package/dist/createPluginSystemHelpers.d.ts +37 -0
  23. package/dist/createPluginSystemHelpers.js +72 -0
  24. package/dist/createPluginSystemHelpers.js.map +1 -0
  25. package/dist/createPluginSystemRunner.d.ts +58 -0
  26. package/dist/createPluginSystemRunner.js +128 -0
  27. package/dist/createPluginSystemRunner.js.map +1 -0
  28. package/dist/definePlugin.d.ts +30 -0
  29. package/dist/definePlugin.js +5 -0
  30. package/dist/definePlugin.js.map +1 -0
  31. package/dist/fs/buildCustomTypeDirectoryPath.d.ts +5 -0
  32. package/dist/fs/buildCustomTypeDirectoryPath.js +9 -0
  33. package/dist/fs/buildCustomTypeDirectoryPath.js.map +1 -0
  34. package/dist/fs/buildCustomTypeFilePath.d.ts +5 -0
  35. package/dist/fs/buildCustomTypeFilePath.js +9 -0
  36. package/dist/fs/buildCustomTypeFilePath.js.map +1 -0
  37. package/dist/fs/buildCustomTypeLibraryDirectoryPath.d.ts +6 -0
  38. package/dist/fs/buildCustomTypeLibraryDirectoryPath.js +8 -0
  39. package/dist/fs/buildCustomTypeLibraryDirectoryPath.js.map +1 -0
  40. package/dist/fs/buildSliceDirectoryPath.d.ts +4 -0
  41. package/dist/fs/buildSliceDirectoryPath.js +12 -0
  42. package/dist/fs/buildSliceDirectoryPath.js.map +1 -0
  43. package/dist/fs/buildSliceFilePath.d.ts +5 -0
  44. package/dist/fs/buildSliceFilePath.js +9 -0
  45. package/dist/fs/buildSliceFilePath.js.map +1 -0
  46. package/dist/fs/buildSliceLibraryDirectoryPath.d.ts +7 -0
  47. package/dist/fs/buildSliceLibraryDirectoryPath.js +8 -0
  48. package/dist/fs/buildSliceLibraryDirectoryPath.js.map +1 -0
  49. package/dist/fs/checkHasProjectFile.d.ts +6 -0
  50. package/dist/fs/checkHasProjectFile.js +9 -0
  51. package/dist/fs/checkHasProjectFile.js.map +1 -0
  52. package/dist/fs/checkIsTypeScriptProject.d.ts +4 -0
  53. package/dist/fs/checkIsTypeScriptProject.js +12 -0
  54. package/dist/fs/checkIsTypeScriptProject.js.map +1 -0
  55. package/dist/fs/constants.d.ts +9 -0
  56. package/dist/fs/constants.js +15 -0
  57. package/dist/fs/constants.js.map +1 -0
  58. package/dist/fs/deleteCustomTypeDirectory.d.ts +3 -0
  59. package/dist/fs/deleteCustomTypeDirectory.js +18 -0
  60. package/dist/fs/deleteCustomTypeDirectory.js.map +1 -0
  61. package/dist/fs/deleteProjectFile.d.ts +6 -0
  62. package/dist/fs/deleteProjectFile.js +10 -0
  63. package/dist/fs/deleteProjectFile.js.map +1 -0
  64. package/dist/fs/deleteSliceDirectory.d.ts +3 -0
  65. package/dist/fs/deleteSliceDirectory.js +18 -0
  66. package/dist/fs/deleteSliceDirectory.js.map +1 -0
  67. package/dist/fs/deleteSliceFile.d.ts +3 -0
  68. package/dist/fs/deleteSliceFile.js +18 -0
  69. package/dist/fs/deleteSliceFile.js.map +1 -0
  70. package/dist/fs/index.d.ts +49 -0
  71. package/dist/fs/index.js +53 -0
  72. package/dist/fs/index.js.map +1 -0
  73. package/dist/fs/lib/checkPathExists.d.ts +2 -0
  74. package/dist/fs/lib/checkPathExists.js +13 -0
  75. package/dist/fs/lib/checkPathExists.js.map +1 -0
  76. package/dist/fs/lib/fsLimit.d.ts +31 -0
  77. package/dist/fs/lib/fsLimit.js +23 -0
  78. package/dist/fs/lib/fsLimit.js.map +1 -0
  79. package/dist/fs/lib/isCustomTypeModel.d.ts +2 -0
  80. package/dist/fs/lib/isCustomTypeModel.js +7 -0
  81. package/dist/fs/lib/isCustomTypeModel.js.map +1 -0
  82. package/dist/fs/lib/isSharedSliceModel.d.ts +2 -0
  83. package/dist/fs/lib/isSharedSliceModel.js +7 -0
  84. package/dist/fs/lib/isSharedSliceModel.js.map +1 -0
  85. package/dist/fs/lib/pascalCase.d.ts +8 -0
  86. package/dist/fs/lib/pascalCase.js +10 -0
  87. package/dist/fs/lib/pascalCase.js.map +1 -0
  88. package/dist/fs/lib/readJSONFile.d.ts +1 -0
  89. package/dist/fs/lib/readJSONFile.js +9 -0
  90. package/dist/fs/lib/readJSONFile.js.map +1 -0
  91. package/dist/fs/lib/resolveSliceModel.d.ts +10 -0
  92. package/dist/fs/lib/resolveSliceModel.js +14 -0
  93. package/dist/fs/lib/resolveSliceModel.js.map +1 -0
  94. package/dist/fs/readCustomTypeFile.d.ts +8 -0
  95. package/dist/fs/readCustomTypeFile.js +14 -0
  96. package/dist/fs/readCustomTypeFile.js.map +1 -0
  97. package/dist/fs/readCustomTypeLibrary.d.ts +6 -0
  98. package/dist/fs/readCustomTypeLibrary.js +53 -0
  99. package/dist/fs/readCustomTypeLibrary.js.map +1 -0
  100. package/dist/fs/readCustomTypeModel.d.ts +7 -0
  101. package/dist/fs/readCustomTypeModel.js +16 -0
  102. package/dist/fs/readCustomTypeModel.js.map +1 -0
  103. package/dist/fs/readProjectFile.d.ts +12 -0
  104. package/dist/fs/readProjectFile.js +9 -0
  105. package/dist/fs/readProjectFile.js.map +1 -0
  106. package/dist/fs/readSliceFile.d.ts +8 -0
  107. package/dist/fs/readSliceFile.js +17 -0
  108. package/dist/fs/readSliceFile.js.map +1 -0
  109. package/dist/fs/readSliceLibrary.d.ts +7 -0
  110. package/dist/fs/readSliceLibrary.js +56 -0
  111. package/dist/fs/readSliceLibrary.js.map +1 -0
  112. package/dist/fs/readSliceModel.d.ts +11 -0
  113. package/dist/fs/readSliceModel.js +46 -0
  114. package/dist/fs/readSliceModel.js.map +1 -0
  115. package/dist/fs/renameCustomType.d.ts +3 -0
  116. package/dist/fs/renameCustomType.js +8 -0
  117. package/dist/fs/renameCustomType.js.map +1 -0
  118. package/dist/fs/renameSlice.d.ts +7 -0
  119. package/dist/fs/renameSlice.js +22 -0
  120. package/dist/fs/renameSlice.js.map +1 -0
  121. package/dist/fs/types.d.ts +3 -0
  122. package/dist/fs/upsertGlobalTypeScriptTypes.d.ts +16 -0
  123. package/dist/fs/upsertGlobalTypeScriptTypes.js +37 -0
  124. package/dist/fs/upsertGlobalTypeScriptTypes.js.map +1 -0
  125. package/dist/fs/writeCustomTypeFile.d.ts +9 -0
  126. package/dist/fs/writeCustomTypeFile.js +18 -0
  127. package/dist/fs/writeCustomTypeFile.js.map +1 -0
  128. package/dist/fs/writeCustomTypeModel.d.ts +12 -0
  129. package/dist/fs/writeCustomTypeModel.js +14 -0
  130. package/dist/fs/writeCustomTypeModel.js.map +1 -0
  131. package/dist/fs/writeProjectFile.d.ts +15 -0
  132. package/dist/fs/writeProjectFile.js +16 -0
  133. package/dist/fs/writeProjectFile.js.map +1 -0
  134. package/dist/fs/writeSliceFile.d.ts +9 -0
  135. package/dist/fs/writeSliceFile.js +18 -0
  136. package/dist/fs/writeSliceFile.js.map +1 -0
  137. package/dist/fs/writeSliceModel.d.ts +11 -0
  138. package/dist/fs/writeSliceModel.js +13 -0
  139. package/dist/fs/writeSliceModel.js.map +1 -0
  140. package/dist/hooks/customType-create.d.ts +28 -0
  141. package/dist/hooks/customType-delete.d.ts +26 -0
  142. package/dist/hooks/customType-read.d.ts +32 -0
  143. package/dist/hooks/customType-rename.d.ts +29 -0
  144. package/dist/hooks/customType-update.d.ts +29 -0
  145. package/dist/hooks/customTypeLibrary-read.d.ts +25 -0
  146. package/dist/hooks/debug.d.ts +22 -0
  147. package/dist/hooks/project-init.d.ts +35 -0
  148. package/dist/hooks/slice-create.d.ts +30 -0
  149. package/dist/hooks/slice-delete.d.ts +26 -0
  150. package/dist/hooks/slice-read.d.ts +32 -0
  151. package/dist/hooks/slice-rename.d.ts +26 -0
  152. package/dist/hooks/slice-update.d.ts +29 -0
  153. package/dist/hooks/sliceLibrary-read.d.ts +27 -0
  154. package/dist/index.d.ts +24 -0
  155. package/dist/index.js +15 -0
  156. package/dist/index.js.map +1 -0
  157. package/dist/lib/DecodeError.d.ts +13 -0
  158. package/dist/lib/DecodeError.js +21 -0
  159. package/dist/lib/DecodeError.js.map +1 -0
  160. package/dist/lib/HookSystem.d.ts +93 -0
  161. package/dist/lib/HookSystem.js +121 -0
  162. package/dist/lib/HookSystem.js.map +1 -0
  163. package/dist/lib/decode.d.ts +10 -0
  164. package/dist/lib/decode.js +16 -0
  165. package/dist/lib/decode.js.map +1 -0
  166. package/dist/lib/decodePrismicConfig.d.ts +3 -0
  167. package/dist/lib/decodePrismicConfig.js +22 -0
  168. package/dist/lib/decodePrismicConfig.js.map +1 -0
  169. package/dist/types.d.ts +129 -0
  170. package/dist/types.js +21 -0
  171. package/dist/types.js.map +1 -0
  172. package/package.json +90 -0
  173. package/src/createPluginHookSystem.ts +9 -0
  174. package/src/createPluginSystemActions.ts +188 -0
  175. package/src/createPluginSystemContext.ts +51 -0
  176. package/src/createPluginSystemHelpers.ts +129 -0
  177. package/src/createPluginSystemRunner.ts +216 -0
  178. package/src/definePlugin.ts +48 -0
  179. package/src/fs/buildCustomTypeDirectoryPath.ts +19 -0
  180. package/src/fs/buildCustomTypeFilePath.ts +16 -0
  181. package/src/fs/buildCustomTypeLibraryDirectoryPath.ts +16 -0
  182. package/src/fs/buildSliceDirectoryPath.ts +25 -0
  183. package/src/fs/buildSliceFilePath.ts +16 -0
  184. package/src/fs/buildSliceLibraryDirectoryPath.ts +17 -0
  185. package/src/fs/checkHasProjectFile.ts +16 -0
  186. package/src/fs/checkIsTypeScriptProject.ts +16 -0
  187. package/src/fs/constants.ts +15 -0
  188. package/src/fs/deleteCustomTypeDirectory.ts +24 -0
  189. package/src/fs/deleteProjectFile.ts +18 -0
  190. package/src/fs/deleteSliceDirectory.ts +24 -0
  191. package/src/fs/deleteSliceFile.ts +24 -0
  192. package/src/fs/index.ts +103 -0
  193. package/src/fs/lib/checkPathExists.ts +13 -0
  194. package/src/fs/lib/fsLimit.ts +72 -0
  195. package/src/fs/lib/isCustomTypeModel.ts +7 -0
  196. package/src/fs/lib/isSharedSliceModel.ts +14 -0
  197. package/src/fs/lib/pascalCase.ts +17 -0
  198. package/src/fs/lib/readJSONFile.ts +7 -0
  199. package/src/fs/lib/resolveSliceModel.ts +28 -0
  200. package/src/fs/readCustomTypeFile.ts +27 -0
  201. package/src/fs/readCustomTypeLibrary.ts +86 -0
  202. package/src/fs/readCustomTypeModel.ts +27 -0
  203. package/src/fs/readProjectFile.ts +24 -0
  204. package/src/fs/readSliceFile.ts +27 -0
  205. package/src/fs/readSliceLibrary.ts +91 -0
  206. package/src/fs/readSliceModel.ts +90 -0
  207. package/src/fs/renameCustomType.ts +12 -0
  208. package/src/fs/renameSlice.ts +35 -0
  209. package/src/fs/types.ts +5 -0
  210. package/src/fs/upsertGlobalTypeScriptTypes.ts +64 -0
  211. package/src/fs/writeCustomTypeFile.ts +30 -0
  212. package/src/fs/writeCustomTypeModel.ts +31 -0
  213. package/src/fs/writeProjectFile.ts +39 -0
  214. package/src/fs/writeSliceFile.ts +30 -0
  215. package/src/fs/writeSliceModel.ts +26 -0
  216. package/src/hooks/customType-create.ts +42 -0
  217. package/src/hooks/customType-delete.ts +40 -0
  218. package/src/hooks/customType-read.ts +46 -0
  219. package/src/hooks/customType-rename.ts +43 -0
  220. package/src/hooks/customType-update.ts +43 -0
  221. package/src/hooks/customTypeLibrary-read.ts +38 -0
  222. package/src/hooks/debug.ts +31 -0
  223. package/src/hooks/project-init.ts +48 -0
  224. package/src/hooks/slice-create.ts +44 -0
  225. package/src/hooks/slice-delete.ts +40 -0
  226. package/src/hooks/slice-read.ts +44 -0
  227. package/src/hooks/slice-rename.ts +40 -0
  228. package/src/hooks/slice-update.ts +43 -0
  229. package/src/hooks/sliceLibrary-read.ts +41 -0
  230. package/src/index.ts +141 -0
  231. package/src/lib/DecodeError.ts +31 -0
  232. package/src/lib/HookSystem.ts +277 -0
  233. package/src/lib/decode.ts +30 -0
  234. package/src/lib/decodePrismicConfig.ts +26 -0
  235. 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
+ };