@plasmicpkgs/plasmic-strapi 0.1.124 → 0.1.125

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,45 @@
1
+ import registerComponent from '@plasmicapp/host/registerComponent';
2
+ import registerGlobalContext from '@plasmicapp/host/registerGlobalContext';
3
+ import { GlobalContextMeta, ComponentMeta } from '@plasmicapp/host';
4
+ import React, { ReactNode } from 'react';
5
+
6
+ declare function ensure<T>(x: T | null | undefined): T;
7
+ interface StrapiCredentialsProviderProps {
8
+ host?: string;
9
+ token?: string;
10
+ }
11
+ declare const strapiCredentialsProviderMeta: GlobalContextMeta<StrapiCredentialsProviderProps>;
12
+ declare function StrapiCredentialsProvider({ host, token, children, }: React.PropsWithChildren<StrapiCredentialsProviderProps>): React.JSX.Element;
13
+ interface StrapiCollectionProps {
14
+ name?: string;
15
+ children?: ReactNode;
16
+ className?: string;
17
+ noLayout?: boolean;
18
+ noAutoRepeat?: boolean;
19
+ filterField?: string;
20
+ filterValue?: string;
21
+ limit?: number;
22
+ filterParameter?: string;
23
+ setControlContextData?: (data: {
24
+ strapiFields: string[];
25
+ }) => void;
26
+ }
27
+ declare const strapiCollectionMeta: ComponentMeta<StrapiCollectionProps>;
28
+ declare function StrapiCollection({ name, filterParameter, filterValue, filterField, limit, children, className, noLayout, noAutoRepeat, setControlContextData, }: StrapiCollectionProps): React.JSX.Element;
29
+ interface StrapiFieldProps {
30
+ className?: string;
31
+ path?: string;
32
+ setControlContextData?: (data: {
33
+ fields: string[];
34
+ isImage: boolean;
35
+ }) => void;
36
+ }
37
+ declare const strapiFieldMeta: ComponentMeta<StrapiFieldProps>;
38
+ declare function StrapiField({ className, path, setControlContextData, }: StrapiFieldProps): React.JSX.Element;
39
+
40
+ declare function registerAll(loader?: {
41
+ registerComponent: typeof registerComponent;
42
+ registerGlobalContext: typeof registerGlobalContext;
43
+ }): void;
44
+
45
+ export { StrapiCollection, StrapiCredentialsProvider, StrapiField, ensure, registerAll, strapiCollectionMeta, strapiCredentialsProviderMeta, strapiFieldMeta };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,45 @@
1
- import registerComponent from "@plasmicapp/host/registerComponent";
2
- import registerGlobalContext from "@plasmicapp/host/registerGlobalContext";
3
- export declare function registerAll(loader?: {
4
- registerComponent: typeof registerComponent;
5
- registerGlobalContext: typeof registerGlobalContext;
6
- }): void;
7
- export * from "./strapi";
1
+ import registerComponent from '@plasmicapp/host/registerComponent';
2
+ import registerGlobalContext from '@plasmicapp/host/registerGlobalContext';
3
+ import { GlobalContextMeta, ComponentMeta } from '@plasmicapp/host';
4
+ import React, { ReactNode } from 'react';
5
+
6
+ declare function ensure<T>(x: T | null | undefined): T;
7
+ interface StrapiCredentialsProviderProps {
8
+ host?: string;
9
+ token?: string;
10
+ }
11
+ declare const strapiCredentialsProviderMeta: GlobalContextMeta<StrapiCredentialsProviderProps>;
12
+ declare function StrapiCredentialsProvider({ host, token, children, }: React.PropsWithChildren<StrapiCredentialsProviderProps>): React.JSX.Element;
13
+ interface StrapiCollectionProps {
14
+ name?: string;
15
+ children?: ReactNode;
16
+ className?: string;
17
+ noLayout?: boolean;
18
+ noAutoRepeat?: boolean;
19
+ filterField?: string;
20
+ filterValue?: string;
21
+ limit?: number;
22
+ filterParameter?: string;
23
+ setControlContextData?: (data: {
24
+ strapiFields: string[];
25
+ }) => void;
26
+ }
27
+ declare const strapiCollectionMeta: ComponentMeta<StrapiCollectionProps>;
28
+ declare function StrapiCollection({ name, filterParameter, filterValue, filterField, limit, children, className, noLayout, noAutoRepeat, setControlContextData, }: StrapiCollectionProps): React.JSX.Element;
29
+ interface StrapiFieldProps {
30
+ className?: string;
31
+ path?: string;
32
+ setControlContextData?: (data: {
33
+ fields: string[];
34
+ isImage: boolean;
35
+ }) => void;
36
+ }
37
+ declare const strapiFieldMeta: ComponentMeta<StrapiFieldProps>;
38
+ declare function StrapiField({ className, path, setControlContextData, }: StrapiFieldProps): React.JSX.Element;
39
+
40
+ declare function registerAll(loader?: {
41
+ registerComponent: typeof registerComponent;
42
+ registerGlobalContext: typeof registerGlobalContext;
43
+ }): void;
44
+
45
+ export { StrapiCollection, StrapiCredentialsProvider, StrapiField, ensure, registerAll, strapiCollectionMeta, strapiCredentialsProviderMeta, strapiFieldMeta };
package/dist/index.js CHANGED
@@ -1,8 +1,417 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
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
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
29
 
2
- 'use strict'
30
+ // src/index.tsx
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ StrapiCollection: () => StrapiCollection,
34
+ StrapiCredentialsProvider: () => StrapiCredentialsProvider,
35
+ StrapiField: () => StrapiField,
36
+ ensure: () => ensure,
37
+ registerAll: () => registerAll,
38
+ strapiCollectionMeta: () => strapiCollectionMeta,
39
+ strapiCredentialsProviderMeta: () => strapiCredentialsProviderMeta,
40
+ strapiFieldMeta: () => strapiFieldMeta
41
+ });
42
+ module.exports = __toCommonJS(src_exports);
43
+ var import_registerComponent = __toESM(require("@plasmicapp/host/registerComponent"));
44
+ var import_registerGlobalContext = __toESM(require("@plasmicapp/host/registerGlobalContext"));
3
45
 
4
- if (process.env.NODE_ENV === 'production') {
5
- module.exports = require('./plasmic-strapi.cjs.production.min.js')
6
- } else {
7
- module.exports = require('./plasmic-strapi.cjs.development.js')
46
+ // src/strapi.tsx
47
+ var import_host = require("@plasmicapp/host");
48
+ var import_query = require("@plasmicapp/query");
49
+ var qs = __toESM(require("qs"));
50
+ var import_dlv = __toESM(require("dlv"));
51
+ var import_change_case = require("change-case");
52
+ var import_react = __toESM(require("react"));
53
+
54
+ // src/utils.ts
55
+ var queryParameters = [
56
+ {
57
+ value: "$eq",
58
+ label: "Equal"
59
+ },
60
+ {
61
+ value: "$ne",
62
+ label: "Not equal"
63
+ },
64
+ {
65
+ value: "$lt",
66
+ label: "Less than"
67
+ },
68
+ {
69
+ value: "$lte",
70
+ label: "Less than or equal to"
71
+ },
72
+ {
73
+ value: "$gt",
74
+ label: "Greater than"
75
+ },
76
+ {
77
+ value: "$gte",
78
+ label: "Greater than or equal to"
79
+ },
80
+ {
81
+ value: "$in",
82
+ label: "Included in an array"
83
+ },
84
+ {
85
+ value: "$notIn",
86
+ label: "Not included in an array"
87
+ },
88
+ {
89
+ value: "$contains",
90
+ label: "Contains"
91
+ },
92
+ {
93
+ value: "$notContains",
94
+ label: "Does not contain"
95
+ }
96
+ ];
97
+ var uniq = (xs) => Array.from(new Set(xs));
98
+
99
+ // src/strapi.tsx
100
+ function ensure(x) {
101
+ if (x === null || x === void 0) {
102
+ throw new Error(`Value must not be undefined or null`);
103
+ } else {
104
+ return x;
105
+ }
106
+ }
107
+ var modulePath = "@plasmicpkgs/plasmic-strapi";
108
+ var makeDataProviderName = (collection) => `currentStrapi${(0, import_change_case.pascalCase)(collection)}Item`;
109
+ var CredentialsContext = import_react.default.createContext(void 0);
110
+ var strapiCredentialsProviderMeta = {
111
+ name: "StrapiCredentialsProvider",
112
+ displayName: "Strapi Credentials Provider",
113
+ description: `[See tutorial video](https://www.youtube.com/watch?v=1SLoVY3hkQ4).
114
+
115
+ API token is needed only if data is not publicly readable.
116
+
117
+ Learn how to [get your API token](https://docs.strapi.io/user-docs/latest/settings/managing-global-settings.html#managing-api-tokens).`,
118
+ importName: "StrapiCredentialsProvider",
119
+ importPath: modulePath,
120
+ props: {
121
+ host: {
122
+ type: "string",
123
+ displayName: "Host",
124
+ defaultValueHint: "https://strapi-app.plasmic.app",
125
+ defaultValue: "https://strapi-app.plasmic.app",
126
+ description: "Server where you application is hosted."
127
+ },
128
+ token: {
129
+ type: "string",
130
+ displayName: "API Token",
131
+ description: "API Token (generated in http://yourhost/admin/settings/api-tokens) (or leave blank for unauthenticated usage)."
132
+ }
133
+ }
134
+ };
135
+ function StrapiCredentialsProvider({
136
+ host,
137
+ token,
138
+ children
139
+ }) {
140
+ host = (host == null ? void 0 : host.slice(-1)) === "/" ? host.slice(0, -1) : host;
141
+ return /* @__PURE__ */ import_react.default.createElement(CredentialsContext.Provider, { value: { host, token } }, children);
142
+ }
143
+ var strapiCollectionMeta = {
144
+ name: "StrapiCollection",
145
+ displayName: "Strapi Collection",
146
+ importName: "StrapiCollection",
147
+ importPath: modulePath,
148
+ providesData: true,
149
+ description: "Fetches Strapi data of a given collection, and repeats `children` slot content for each row fetched. [See tutorial video](https://www.youtube.com/watch?v=1SLoVY3hkQ4).",
150
+ defaultStyles: {
151
+ display: "grid",
152
+ gridTemplateColumns: "1fr 1fr 1fr 1fr",
153
+ gridRowGap: "8px",
154
+ gridColumnGap: "8px",
155
+ padding: "8px",
156
+ maxWidth: "100%"
157
+ },
158
+ props: {
159
+ children: {
160
+ type: "slot",
161
+ defaultValue: {
162
+ type: "vbox",
163
+ children: {
164
+ type: "component",
165
+ name: "StrapiField"
166
+ }
167
+ }
168
+ },
169
+ name: {
170
+ type: "string",
171
+ displayName: "Name",
172
+ description: "Name of the collection to be fetched.",
173
+ defaultValueHint: "restaurants"
174
+ },
175
+ filterField: {
176
+ type: "choice",
177
+ displayName: "Filter field",
178
+ description: "Field (from Collection) to filter by",
179
+ options: (props, ctx) => {
180
+ var _a;
181
+ return (_a = ctx == null ? void 0 : ctx.strapiFields) != null ? _a : [];
182
+ },
183
+ hidden: (props, ctx) => !props.name
184
+ },
185
+ filterParameter: {
186
+ type: "choice",
187
+ displayName: "Filter Parameter",
188
+ description: "Field Parameter filter by",
189
+ options: (props, ctx) => {
190
+ return queryParameters.map((item) => ({
191
+ label: item == null ? void 0 : item.label,
192
+ value: item == null ? void 0 : item.value
193
+ }));
194
+ },
195
+ hidden: (props, ctx) => !props.filterField
196
+ },
197
+ filterValue: {
198
+ type: "string",
199
+ displayName: "Filter value",
200
+ description: "Value to filter by, should be of filter field type",
201
+ hidden: (props, ctx) => !props.filterParameter
202
+ },
203
+ limit: {
204
+ type: "number",
205
+ displayName: "Limit",
206
+ description: "Maximum n umber of collections to fetch (0 for unlimited)."
207
+ },
208
+ noLayout: {
209
+ type: "boolean",
210
+ displayName: "No layout",
211
+ description: "When set, Strapi Collection will not layout its children; instead, the layout set on its parent element will be used. Useful if you want to set flex gap or control container tag type.",
212
+ defaultValue: false
213
+ },
214
+ noAutoRepeat: {
215
+ type: "boolean",
216
+ displayName: "No auto-repeat",
217
+ description: "Do not automatically repeat children for every category.",
218
+ defaultValue: false
219
+ }
220
+ }
221
+ };
222
+ function StrapiCollection({
223
+ name,
224
+ filterParameter,
225
+ filterValue,
226
+ filterField,
227
+ limit,
228
+ children,
229
+ className,
230
+ noLayout,
231
+ noAutoRepeat,
232
+ setControlContextData
233
+ }) {
234
+ const creds = ensure((0, import_react.useContext)(CredentialsContext));
235
+ if (!creds.host) {
236
+ return /* @__PURE__ */ import_react.default.createElement("div", null, "Please specify a host.");
237
+ }
238
+ const query = creds.host + "/api/" + name;
239
+ const cacheKey = JSON.stringify({
240
+ creds,
241
+ name,
242
+ filterField,
243
+ filterValue,
244
+ filterParameter
245
+ });
246
+ const data = (0, import_query.usePlasmicQueryData)(cacheKey, async () => {
247
+ if (!query) {
248
+ return null;
249
+ }
250
+ const requestInit = { method: "GET" };
251
+ if (creds.token) {
252
+ requestInit.headers = { Authorization: "Bearer " + creds.token };
253
+ }
254
+ const queryParams = qs.stringify({
255
+ ...filterField && filterParameter && filterValue ? {
256
+ filters: {
257
+ [filterField]: {
258
+ [filterParameter]: filterValue
259
+ }
260
+ }
261
+ } : {},
262
+ populate: "*"
263
+ });
264
+ const resp = await fetch(`${query}?${queryParams}`, requestInit);
265
+ return resp.json();
266
+ });
267
+ if (!(data == null ? void 0 : data.data)) {
268
+ return /* @__PURE__ */ import_react.default.createElement("div", null, "Please configure the Strapi provider with a valid host and token.");
269
+ }
270
+ if (!(0, import_dlv.default)(data.data, ["data"])) {
271
+ return /* @__PURE__ */ import_react.default.createElement("div", null, "Please specify a valid collection.");
272
+ }
273
+ const collectionData = (0, import_dlv.default)(data.data, ["data"]);
274
+ const filterFieds = collectionData.flatMap((item) => {
275
+ const attributes = (0, import_dlv.default)(item, ["attributes"]);
276
+ const displayableFields = Object.keys(attributes).filter((field) => {
277
+ var _a, _b;
278
+ const value = attributes[field];
279
+ const maybeMime = (_b = (_a = value == null ? void 0 : value.data) == null ? void 0 : _a.attributes) == null ? void 0 : _b.mime;
280
+ return typeof value !== "object" || typeof maybeMime === "string" && maybeMime.startsWith("image");
281
+ });
282
+ return displayableFields;
283
+ });
284
+ setControlContextData == null ? void 0 : setControlContextData({
285
+ strapiFields: uniq(filterFieds != null ? filterFieds : [])
286
+ });
287
+ if (filterParameter && !filterValue && !filterField) {
288
+ return /* @__PURE__ */ import_react.default.createElement("div", null, "Please specify a Filter Field and a Filter Value");
289
+ }
290
+ if (!filterParameter && filterValue && !filterField) {
291
+ return /* @__PURE__ */ import_react.default.createElement("div", null, "Please specify a Filter Parameter and a Filter Field");
292
+ }
293
+ if (!filterParameter && !filterValue && filterField) {
294
+ return /* @__PURE__ */ import_react.default.createElement("div", null, "Please specify a Filter Parameter and a Filter Value");
295
+ }
296
+ if (filterParameter && filterValue && !filterField) {
297
+ return /* @__PURE__ */ import_react.default.createElement("div", null, "Please specify a Filter Field");
298
+ }
299
+ if (!filterParameter && filterValue && filterField) {
300
+ return /* @__PURE__ */ import_react.default.createElement("div", null, "Please specify a Filter Parameter");
301
+ }
302
+ if (filterParameter && !filterValue && filterField) {
303
+ return /* @__PURE__ */ import_react.default.createElement("div", null, "Please specify a Filter Value");
304
+ }
305
+ const collection = limit > 0 ? collectionData.slice(0, limit) : collectionData;
306
+ if (collection.length === 0) {
307
+ return /* @__PURE__ */ import_react.default.createElement("div", null, "No collection found ");
308
+ }
309
+ const repElements = noAutoRepeat ? children : collection.map((item, index) => /* @__PURE__ */ import_react.default.createElement(
310
+ import_host.DataProvider,
311
+ {
312
+ key: item.id,
313
+ name: "strapiItem",
314
+ data: item,
315
+ hidden: true
316
+ },
317
+ /* @__PURE__ */ import_react.default.createElement(import_host.DataProvider, { name: makeDataProviderName(name), data: item }, (0, import_host.repeatedElement)(index, children))
318
+ ));
319
+ return /* @__PURE__ */ import_react.default.createElement(import_host.DataProvider, { name: "strapiItems", data: collection }, noLayout ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, " ", repElements, " ") : /* @__PURE__ */ import_react.default.createElement("div", { className }, " ", repElements, " "));
320
+ }
321
+ var strapiFieldMeta = {
322
+ name: "StrapiField",
323
+ displayName: "Strapi Field",
324
+ importName: "StrapiField",
325
+ importPath: modulePath,
326
+ props: {
327
+ path: {
328
+ type: "choice",
329
+ options: (props, ctx) => {
330
+ var _a;
331
+ return (_a = ctx == null ? void 0 : ctx.fields) != null ? _a : [];
332
+ },
333
+ displayName: "Field",
334
+ description: "Field name"
335
+ }
336
+ }
337
+ };
338
+ function StrapiField({
339
+ className,
340
+ path,
341
+ setControlContextData
342
+ }) {
343
+ var _a, _b;
344
+ const item = (0, import_host.useSelector)("strapiItem");
345
+ if (!item) {
346
+ return /* @__PURE__ */ import_react.default.createElement("div", null, "StrapiField must be used within a StrapiCollection");
347
+ }
348
+ const attributes = (0, import_dlv.default)(item, ["attributes"]);
349
+ const displayableFields = Object.keys(attributes).filter((field) => {
350
+ var _a2, _b2;
351
+ const value = attributes[field];
352
+ const maybeMime2 = (_b2 = (_a2 = value.data) == null ? void 0 : _a2.attributes) == null ? void 0 : _b2.mime;
353
+ return typeof value !== "object" || typeof maybeMime2 === "string" && maybeMime2.startsWith("image");
354
+ });
355
+ setControlContextData == null ? void 0 : setControlContextData({
356
+ fields: displayableFields,
357
+ isImage: false
358
+ });
359
+ if (!path) {
360
+ return /* @__PURE__ */ import_react.default.createElement("div", null, "StrapiField must specify a field name.");
361
+ }
362
+ const data = (0, import_dlv.default)(item, ["attributes", path]);
363
+ const maybeMime = (_b = (_a = data == null ? void 0 : data.data) == null ? void 0 : _a.attributes) == null ? void 0 : _b.mime;
364
+ setControlContextData == null ? void 0 : setControlContextData({
365
+ fields: displayableFields,
366
+ isImage: typeof maybeMime === "string" && maybeMime.startsWith("image")
367
+ });
368
+ if (!data) {
369
+ return /* @__PURE__ */ import_react.default.createElement("div", null, "Please specify a valid field name.");
370
+ } else if (typeof maybeMime === "string" && maybeMime.startsWith("image")) {
371
+ const creds = ensure((0, import_react.useContext)(CredentialsContext));
372
+ const attrs = data.data.attributes;
373
+ const img_url = attrs.url.startsWith("http") ? attrs.url : creds.host + attrs.url;
374
+ const img_width = attrs.width;
375
+ const img_height = attrs.height;
376
+ return /* @__PURE__ */ import_react.default.createElement(
377
+ "img",
378
+ {
379
+ className,
380
+ src: img_url,
381
+ width: 300,
382
+ height: 300 * img_height / img_width
383
+ }
384
+ );
385
+ } else {
386
+ return /* @__PURE__ */ import_react.default.createElement("div", { className }, data);
387
+ }
388
+ }
389
+
390
+ // src/index.tsx
391
+ function registerAll(loader) {
392
+ const _registerComponent = (Component, defaultMeta) => {
393
+ if (loader) {
394
+ loader.registerComponent(Component, defaultMeta);
395
+ } else {
396
+ (0, import_registerComponent.default)(Component, defaultMeta);
397
+ }
398
+ };
399
+ if (loader) {
400
+ loader.registerGlobalContext(StrapiCredentialsProvider, strapiCredentialsProviderMeta);
401
+ } else {
402
+ (0, import_registerGlobalContext.default)(StrapiCredentialsProvider, strapiCredentialsProviderMeta);
403
+ }
404
+ _registerComponent(StrapiCollection, strapiCollectionMeta);
405
+ _registerComponent(StrapiField, strapiFieldMeta);
8
406
  }
407
+ // Annotate the CommonJS export names for ESM import in node:
408
+ 0 && (module.exports = {
409
+ StrapiCollection,
410
+ StrapiCredentialsProvider,
411
+ StrapiField,
412
+ ensure,
413
+ registerAll,
414
+ strapiCollectionMeta,
415
+ strapiCredentialsProviderMeta,
416
+ strapiFieldMeta
417
+ });