@matvdt/js-toolkit 0.0.1-canary.2 → 0.0.1-canary.20

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.
@@ -0,0 +1,51 @@
1
+ // src/payload-cms/utils/is-populated.ts
2
+ function isPopulated(relation) {
3
+ return typeof relation !== "string";
4
+ }
5
+
6
+ // src/payload-cms/utils/as-populated.ts
7
+ function asPopulated(relation) {
8
+ return isPopulated(relation) ? relation : void 0;
9
+ }
10
+
11
+ // src/payload-cms/utils/is-object-id.ts
12
+ var MONGO_ID_REGEX = /^[0-9a-fA-F]{24}$/;
13
+ function isObjectId(value) {
14
+ return typeof value === "string" && MONGO_ID_REGEX.test(value);
15
+ }
16
+
17
+ // src/payload-cms/utils/assert-populated.ts
18
+ function assertPopulated(field, fieldName = "Field") {
19
+ if (field === null || field === void 0) {
20
+ throw new Error(`${fieldName} is undefined or null`);
21
+ }
22
+ if (isObjectId(field)) {
23
+ throw new Error(`${fieldName} is not populated (received ID: ${String(field)})`);
24
+ }
25
+ }
26
+
27
+ // src/payload-cms/utils/has-id.ts
28
+ function hasId(item) {
29
+ return item.id !== null && item.id !== void 0;
30
+ }
31
+
32
+ // src/payload-cms/utils/unwrap-populated.ts
33
+ function unwrapPopulated(value, message = "Value is missing or not populated") {
34
+ if (value === null || value === void 0) {
35
+ throw new Error(message);
36
+ }
37
+ if (typeof value === "string" && isObjectId(value)) {
38
+ throw new Error(message);
39
+ }
40
+ return value;
41
+ }
42
+
43
+ export {
44
+ isPopulated,
45
+ asPopulated,
46
+ MONGO_ID_REGEX,
47
+ isObjectId,
48
+ assertPopulated,
49
+ hasId,
50
+ unwrapPopulated
51
+ };
package/dist/index.cjs CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/index.ts
@@ -33,25 +23,13 @@ __export(index_exports, {
33
23
  MONGO_ID_REGEX: () => MONGO_ID_REGEX,
34
24
  asPopulated: () => asPopulated,
35
25
  assertPopulated: () => assertPopulated,
36
- createCollection: () => createCollection,
37
- createGlobal: () => createGlobal,
38
- db: () => db,
39
- getPayload: () => getPayload,
26
+ hasId: () => hasId,
40
27
  isObjectId: () => isObjectId,
41
- isPopulated: () => isPopulated
28
+ isPopulated: () => isPopulated,
29
+ unwrapPopulated: () => unwrapPopulated
42
30
  });
43
31
  module.exports = __toCommonJS(index_exports);
44
32
 
45
- // src/payload-cms/client/get-payload.ts
46
- var import_payload = require("payload");
47
- var getPayload = async (config) => {
48
- if (config) {
49
- return (0, import_payload.getPayload)({ config });
50
- }
51
- const defaultConfig = await import("@payload-config");
52
- return (0, import_payload.getPayload)({ config: defaultConfig });
53
- };
54
-
55
33
  // src/payload-cms/utils/is-populated.ts
56
34
  function isPopulated(relation) {
57
35
  return typeof relation !== "string";
@@ -78,120 +56,28 @@ function assertPopulated(field, fieldName = "Field") {
78
56
  }
79
57
  }
80
58
 
81
- // src/payload-cms/utils/db.ts
82
- var import_react = require("react");
83
- var getClient = (0, import_react.cache)(async () => {
84
- return await getPayload();
85
- });
86
- var db = {
87
- findOne: async (collection, where) => {
88
- const payload = await getClient();
89
- const res = await payload.find({ collection, where, limit: 1 });
90
- return res.docs[0] ?? null;
91
- },
92
- findBySlug: async (collection, slug) => {
93
- return db.findOne(collection, { slug: { equals: slug } });
94
- },
95
- findAll: async (collection, options) => {
96
- const payload = await getClient();
97
- const res = await payload.find({
98
- collection,
99
- limit: options?.limit ?? 100,
100
- ...options
101
- });
102
- return res.docs;
103
- },
104
- findByID: async (collection, id) => {
105
- const payload = await getClient();
106
- return payload.findByID({ collection, id });
107
- },
108
- create: async (collection, data, options) => {
109
- const payload = await getClient();
110
- return payload.create({ collection, data, ...options });
111
- },
112
- update: async (collection, id, data, options) => {
113
- const payload = await getClient();
114
- return payload.update({
115
- collection,
116
- id,
117
- data,
118
- ...options
119
- });
120
- },
121
- updateMany: async (collection, where, data, options) => {
122
- const payload = await getClient();
123
- return payload.update({
124
- collection,
125
- where,
126
- data,
127
- ...options,
128
- id: void 0
129
- });
130
- },
131
- delete: async (collection, id, options) => {
132
- const payload = await getClient();
133
- return payload.delete({
134
- collection,
135
- id,
136
- where: void 0,
137
- ...options
138
- });
139
- },
140
- deleteMany: async (collection, where, options) => {
141
- const payload = await getClient();
142
- return payload.delete({
143
- collection,
144
- where,
145
- id: void 0,
146
- ...options
147
- });
148
- },
149
- global: async (slug) => {
150
- const payload = await getClient();
151
- return payload.findGlobal({ slug });
152
- },
153
- raw: getClient
154
- };
155
-
156
- // src/payload-cms/utils/create-collection.ts
157
- function createCollection(collection, extend) {
158
- const base = {
159
- findOne: (where) => db.findOne(collection, where),
160
- findByID: (id) => db.findByID(collection, id),
161
- findBySlug: (slug) => db.findBySlug(collection, slug),
162
- findAll: (options) => db.findAll(collection, options),
163
- update: (id, data, options) => db.update(collection, id, data, options),
164
- updateMany: (where, data, options) => db.updateMany(collection, where, data, options),
165
- delete: (id, options) => db.delete(collection, id, options),
166
- deleteMany: (where, options) => db.deleteMany(collection, where, options)
167
- };
168
- const extended = extend ? extend(base) : void 0;
169
- return {
170
- ...base,
171
- ...extended
172
- };
59
+ // src/payload-cms/utils/has-id.ts
60
+ function hasId(item) {
61
+ return item.id !== null && item.id !== void 0;
173
62
  }
174
63
 
175
- // src/payload-cms/utils/create-global.ts
176
- function createGlobal(slug, extend) {
177
- const base = {
178
- get: () => db.global(slug)
179
- };
180
- const extended = extend ? extend(base) : void 0;
181
- return {
182
- ...base,
183
- ...extended
184
- };
64
+ // src/payload-cms/utils/unwrap-populated.ts
65
+ function unwrapPopulated(value, message = "Value is missing or not populated") {
66
+ if (value === null || value === void 0) {
67
+ throw new Error(message);
68
+ }
69
+ if (typeof value === "string" && isObjectId(value)) {
70
+ throw new Error(message);
71
+ }
72
+ return value;
185
73
  }
186
74
  // Annotate the CommonJS export names for ESM import in node:
187
75
  0 && (module.exports = {
188
76
  MONGO_ID_REGEX,
189
77
  asPopulated,
190
78
  assertPopulated,
191
- createCollection,
192
- createGlobal,
193
- db,
194
- getPayload,
79
+ hasId,
195
80
  isObjectId,
196
- isPopulated
81
+ isPopulated,
82
+ unwrapPopulated
197
83
  });
package/dist/index.js CHANGED
@@ -1,26 +1,19 @@
1
- import "./chunk-EMH4G65N.js";
2
- import "./chunk-NSY6BIWE.js";
1
+ import "./chunk-7GZ4G2P3.js";
3
2
  import {
4
3
  MONGO_ID_REGEX,
5
4
  asPopulated,
6
5
  assertPopulated,
7
- createCollection,
8
- createGlobal,
9
- db,
6
+ hasId,
10
7
  isObjectId,
11
- isPopulated
12
- } from "./chunk-EZIPIKRW.js";
13
- import {
14
- getPayload
15
- } from "./chunk-CWTTD7TW.js";
8
+ isPopulated,
9
+ unwrapPopulated
10
+ } from "./chunk-UYAULMAI.js";
16
11
  export {
17
12
  MONGO_ID_REGEX,
18
13
  asPopulated,
19
14
  assertPopulated,
20
- createCollection,
21
- createGlobal,
22
- db,
23
- getPayload,
15
+ hasId,
24
16
  isObjectId,
25
- isPopulated
17
+ isPopulated,
18
+ unwrapPopulated
26
19
  };
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/payload-cms/index.ts
@@ -33,25 +23,13 @@ __export(payload_cms_exports, {
33
23
  MONGO_ID_REGEX: () => MONGO_ID_REGEX,
34
24
  asPopulated: () => asPopulated,
35
25
  assertPopulated: () => assertPopulated,
36
- createCollection: () => createCollection,
37
- createGlobal: () => createGlobal,
38
- db: () => db,
39
- getPayload: () => getPayload,
26
+ hasId: () => hasId,
40
27
  isObjectId: () => isObjectId,
41
- isPopulated: () => isPopulated
28
+ isPopulated: () => isPopulated,
29
+ unwrapPopulated: () => unwrapPopulated
42
30
  });
43
31
  module.exports = __toCommonJS(payload_cms_exports);
44
32
 
45
- // src/payload-cms/client/get-payload.ts
46
- var import_payload = require("payload");
47
- var getPayload = async (config) => {
48
- if (config) {
49
- return (0, import_payload.getPayload)({ config });
50
- }
51
- const defaultConfig = await import("@payload-config");
52
- return (0, import_payload.getPayload)({ config: defaultConfig });
53
- };
54
-
55
33
  // src/payload-cms/utils/is-populated.ts
56
34
  function isPopulated(relation) {
57
35
  return typeof relation !== "string";
@@ -78,120 +56,28 @@ function assertPopulated(field, fieldName = "Field") {
78
56
  }
79
57
  }
80
58
 
81
- // src/payload-cms/utils/db.ts
82
- var import_react = require("react");
83
- var getClient = (0, import_react.cache)(async () => {
84
- return await getPayload();
85
- });
86
- var db = {
87
- findOne: async (collection, where) => {
88
- const payload = await getClient();
89
- const res = await payload.find({ collection, where, limit: 1 });
90
- return res.docs[0] ?? null;
91
- },
92
- findBySlug: async (collection, slug) => {
93
- return db.findOne(collection, { slug: { equals: slug } });
94
- },
95
- findAll: async (collection, options) => {
96
- const payload = await getClient();
97
- const res = await payload.find({
98
- collection,
99
- limit: options?.limit ?? 100,
100
- ...options
101
- });
102
- return res.docs;
103
- },
104
- findByID: async (collection, id) => {
105
- const payload = await getClient();
106
- return payload.findByID({ collection, id });
107
- },
108
- create: async (collection, data, options) => {
109
- const payload = await getClient();
110
- return payload.create({ collection, data, ...options });
111
- },
112
- update: async (collection, id, data, options) => {
113
- const payload = await getClient();
114
- return payload.update({
115
- collection,
116
- id,
117
- data,
118
- ...options
119
- });
120
- },
121
- updateMany: async (collection, where, data, options) => {
122
- const payload = await getClient();
123
- return payload.update({
124
- collection,
125
- where,
126
- data,
127
- ...options,
128
- id: void 0
129
- });
130
- },
131
- delete: async (collection, id, options) => {
132
- const payload = await getClient();
133
- return payload.delete({
134
- collection,
135
- id,
136
- where: void 0,
137
- ...options
138
- });
139
- },
140
- deleteMany: async (collection, where, options) => {
141
- const payload = await getClient();
142
- return payload.delete({
143
- collection,
144
- where,
145
- id: void 0,
146
- ...options
147
- });
148
- },
149
- global: async (slug) => {
150
- const payload = await getClient();
151
- return payload.findGlobal({ slug });
152
- },
153
- raw: getClient
154
- };
155
-
156
- // src/payload-cms/utils/create-collection.ts
157
- function createCollection(collection, extend) {
158
- const base = {
159
- findOne: (where) => db.findOne(collection, where),
160
- findByID: (id) => db.findByID(collection, id),
161
- findBySlug: (slug) => db.findBySlug(collection, slug),
162
- findAll: (options) => db.findAll(collection, options),
163
- update: (id, data, options) => db.update(collection, id, data, options),
164
- updateMany: (where, data, options) => db.updateMany(collection, where, data, options),
165
- delete: (id, options) => db.delete(collection, id, options),
166
- deleteMany: (where, options) => db.deleteMany(collection, where, options)
167
- };
168
- const extended = extend ? extend(base) : void 0;
169
- return {
170
- ...base,
171
- ...extended
172
- };
59
+ // src/payload-cms/utils/has-id.ts
60
+ function hasId(item) {
61
+ return item.id !== null && item.id !== void 0;
173
62
  }
174
63
 
175
- // src/payload-cms/utils/create-global.ts
176
- function createGlobal(slug, extend) {
177
- const base = {
178
- get: () => db.global(slug)
179
- };
180
- const extended = extend ? extend(base) : void 0;
181
- return {
182
- ...base,
183
- ...extended
184
- };
64
+ // src/payload-cms/utils/unwrap-populated.ts
65
+ function unwrapPopulated(value, message = "Value is missing or not populated") {
66
+ if (value === null || value === void 0) {
67
+ throw new Error(message);
68
+ }
69
+ if (typeof value === "string" && isObjectId(value)) {
70
+ throw new Error(message);
71
+ }
72
+ return value;
185
73
  }
186
74
  // Annotate the CommonJS export names for ESM import in node:
187
75
  0 && (module.exports = {
188
76
  MONGO_ID_REGEX,
189
77
  asPopulated,
190
78
  assertPopulated,
191
- createCollection,
192
- createGlobal,
193
- db,
194
- getPayload,
79
+ hasId,
195
80
  isObjectId,
196
- isPopulated
81
+ isPopulated,
82
+ unwrapPopulated
197
83
  });
@@ -1,2 +1 @@
1
- export * from './client';
2
1
  export * from './utils';
@@ -1,26 +1,19 @@
1
- import "../chunk-EMH4G65N.js";
2
- import "../chunk-NSY6BIWE.js";
1
+ import "../chunk-7GZ4G2P3.js";
3
2
  import {
4
3
  MONGO_ID_REGEX,
5
4
  asPopulated,
6
5
  assertPopulated,
7
- createCollection,
8
- createGlobal,
9
- db,
6
+ hasId,
10
7
  isObjectId,
11
- isPopulated
12
- } from "../chunk-EZIPIKRW.js";
13
- import {
14
- getPayload
15
- } from "../chunk-CWTTD7TW.js";
8
+ isPopulated,
9
+ unwrapPopulated
10
+ } from "../chunk-UYAULMAI.js";
16
11
  export {
17
12
  MONGO_ID_REGEX,
18
13
  asPopulated,
19
14
  assertPopulated,
20
- createCollection,
21
- createGlobal,
22
- db,
23
- getPayload,
15
+ hasId,
24
16
  isObjectId,
25
- isPopulated
17
+ isPopulated,
18
+ unwrapPopulated
26
19
  };
@@ -0,0 +1,6 @@
1
+ import type { CollectionSlug, GlobalSlug } from 'payload';
2
+ export declare function cacheKey(collection: CollectionSlug, method: string, args: unknown[]): string[];
3
+ export declare function withCache<T>(collection: CollectionSlug, method: string, args: unknown[], tags: string[], fn: () => Promise<T>): Promise<T>;
4
+ export declare function revalidateCollection(collection: CollectionSlug): void;
5
+ export declare function revalidateDoc(collection: CollectionSlug, id: string | number): void;
6
+ export declare function revalidateGlobal(slug: GlobalSlug): void;
@@ -0,0 +1,18 @@
1
+ import type { CollectionSlug, Where } from 'payload';
2
+ import { payload, DeleteOptions, FindByIDOptions, FindOptions, UpdateData, UpdateOptions } from './payload';
3
+ export type BaseCollectionMethods<TSlug extends CollectionSlug> = {
4
+ find: (options?: FindOptions) => ReturnType<typeof payload.find<TSlug>>;
5
+ findOne: (options?: FindOptions) => ReturnType<typeof payload.findOne<TSlug>>;
6
+ findBySlug: (slug: string, options?: FindOptions) => ReturnType<typeof payload.findBySlug<TSlug>>;
7
+ findByID: (id: string | number, options?: FindByIDOptions) => ReturnType<typeof payload.findByID<TSlug>>;
8
+ findAll: (options?: FindOptions) => ReturnType<typeof payload.findAll<TSlug>>;
9
+ update: (id: string | number, data: UpdateData, options?: UpdateOptions) => ReturnType<typeof payload.update<TSlug>>;
10
+ updateMany: (where: Where, data: UpdateData, options?: UpdateOptions) => ReturnType<typeof payload.updateMany<TSlug>>;
11
+ delete: (id: string | number, options?: DeleteOptions) => ReturnType<typeof payload.delete<TSlug>>;
12
+ deleteMany: (where: Where, options?: DeleteOptions) => ReturnType<typeof payload.deleteMany<TSlug>>;
13
+ };
14
+ type ExtensionFunction = (...args: any[]) => any;
15
+ type ExtensionObject = Record<string, unknown>;
16
+ export type CollectionExtension = ExtensionFunction | ExtensionObject;
17
+ export declare function createCollection<TSlug extends CollectionSlug, TExtend extends Record<string, CollectionExtension> = {}>(collection: TSlug, extend?: (base: BaseCollectionMethods<TSlug>) => TExtend): BaseCollectionMethods<TSlug> & TExtend;
18
+ export {};
@@ -1,7 +1,7 @@
1
1
  import type { GlobalSlug } from 'payload';
2
- import { db } from './db';
2
+ import { payload } from './payload';
3
3
  type BaseGlobalMethods<TSlug extends GlobalSlug> = {
4
- get: () => ReturnType<typeof db.global<TSlug>>;
4
+ get: () => ReturnType<typeof payload.global<TSlug>>;
5
5
  };
6
6
  export declare function createGlobal<TSlug extends GlobalSlug, TExtend extends Record<string, (...args: any[]) => any> = {}>(slug: TSlug, extend?: (base: BaseGlobalMethods<TSlug>) => TExtend): BaseGlobalMethods<TSlug> & TExtend;
7
7
  export {};
@@ -0,0 +1,3 @@
1
+ import { type Payload, type SanitizedConfig } from 'payload';
2
+ export declare function configurePayload(config: Promise<SanitizedConfig> | SanitizedConfig): void;
3
+ export declare const getPayload: (config?: Promise<SanitizedConfig> | SanitizedConfig) => Promise<Payload>;