@measured/puck-field-contentful 0.16.0-canary.60fe631 → 0.16.0-canary.6d43ba0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,124 @@
1
+ import { ReactElement } from 'react';
2
+ import { BaseEntry, ContentfulClientApi } from 'contentful';
3
+ export { createClient } from 'contentful';
4
+
5
+ type FieldOption = {
6
+ label: string;
7
+ value: string | number | boolean;
8
+ };
9
+ type FieldOptions = Array<FieldOption> | ReadonlyArray<FieldOption>;
10
+ type BaseField = {
11
+ label?: string;
12
+ };
13
+ type TextField = BaseField & {
14
+ type: "text";
15
+ };
16
+ type NumberField = BaseField & {
17
+ type: "number";
18
+ min?: number;
19
+ max?: number;
20
+ };
21
+ type TextareaField = BaseField & {
22
+ type: "textarea";
23
+ };
24
+ type SelectField = BaseField & {
25
+ type: "select";
26
+ options: FieldOptions;
27
+ };
28
+ type RadioField = BaseField & {
29
+ type: "radio";
30
+ options: FieldOptions;
31
+ };
32
+ type ArrayField<Props extends {
33
+ [key: string]: any;
34
+ } = {
35
+ [key: string]: any;
36
+ }> = BaseField & {
37
+ type: "array";
38
+ arrayFields: {
39
+ [SubPropName in keyof Props[0]]: Field<Props[0][SubPropName]>;
40
+ };
41
+ defaultItemProps?: Props[0];
42
+ getItemSummary?: (item: Props[0], index?: number) => string;
43
+ max?: number;
44
+ min?: number;
45
+ };
46
+ type ObjectField<Props extends {
47
+ [key: string]: any;
48
+ } = {
49
+ [key: string]: any;
50
+ }> = BaseField & {
51
+ type: "object";
52
+ objectFields: Props extends any[] ? never : {
53
+ [SubPropName in keyof Props]: Field<Props[SubPropName]>;
54
+ };
55
+ };
56
+ type Adaptor<AdaptorParams = {}, TableShape extends Record<string, any> = {}, PropShape = TableShape> = {
57
+ name: string;
58
+ fetchList: (adaptorParams?: AdaptorParams) => Promise<TableShape[] | null>;
59
+ mapProp?: (value: TableShape) => PropShape;
60
+ };
61
+ type ExternalFieldWithAdaptor<Props extends {
62
+ [key: string]: any;
63
+ } = {
64
+ [key: string]: any;
65
+ }> = BaseField & {
66
+ type: "external";
67
+ placeholder?: string;
68
+ adaptor: Adaptor<any, any, Props>;
69
+ adaptorParams?: object;
70
+ getItemSummary: (item: Props, index?: number) => string;
71
+ };
72
+ type ExternalField<Props extends {
73
+ [key: string]: any;
74
+ } = {
75
+ [key: string]: any;
76
+ }> = BaseField & {
77
+ type: "external";
78
+ placeholder?: string;
79
+ fetchList: (params: {
80
+ query: string;
81
+ filters: Record<string, any>;
82
+ }) => Promise<any[] | null>;
83
+ mapProp?: (value: any) => Props;
84
+ mapRow?: (value: any) => Record<string, string | number>;
85
+ getItemSummary?: (item: Props, index?: number) => string;
86
+ showSearch?: boolean;
87
+ initialQuery?: string;
88
+ filterFields?: Record<string, Field>;
89
+ initialFilters?: Record<string, any>;
90
+ };
91
+ type CustomField<Props extends any = {}> = BaseField & {
92
+ type: "custom";
93
+ render: (props: {
94
+ field: CustomField<Props>;
95
+ name: string;
96
+ id: string;
97
+ value: Props;
98
+ onChange: (value: Props) => void;
99
+ readOnly?: boolean;
100
+ }) => ReactElement;
101
+ };
102
+ type Field<Props extends any = any> = TextField | NumberField | TextareaField | SelectField | RadioField | ArrayField<Props extends {
103
+ [key: string]: any;
104
+ } ? Props : any> | ObjectField<Props extends {
105
+ [key: string]: any;
106
+ } ? Props : any> | ExternalField<Props extends {
107
+ [key: string]: any;
108
+ } ? Props : any> | ExternalFieldWithAdaptor<Props extends {
109
+ [key: string]: any;
110
+ } ? Props : any> | CustomField<Props>;
111
+
112
+ type Entry<Fields extends Record<string, any> = {}> = BaseEntry & {
113
+ fields: Fields;
114
+ };
115
+ declare function createFieldContentful<T extends Entry = Entry>(contentType: string, options?: {
116
+ client?: ContentfulClientApi<undefined>;
117
+ space?: string;
118
+ accessToken?: string;
119
+ titleField?: string;
120
+ filterFields?: ExternalField["filterFields"];
121
+ initialFilters?: ExternalField["initialFilters"];
122
+ }): ExternalField<T>;
123
+
124
+ export { type Entry, createFieldContentful, createFieldContentful as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,85 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __async = (__this, __arguments, generator) => {
21
+ return new Promise((resolve, reject) => {
22
+ var fulfilled = (value) => {
23
+ try {
24
+ step(generator.next(value));
25
+ } catch (e) {
26
+ reject(e);
27
+ }
28
+ };
29
+ var rejected = (value) => {
30
+ try {
31
+ step(generator.throw(value));
32
+ } catch (e) {
33
+ reject(e);
34
+ }
35
+ };
36
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
37
+ step((generator = generator.apply(__this, __arguments)).next());
38
+ });
39
+ };
40
+
41
+ // ../tsup-config/react-import.js
42
+ import React from "react";
43
+
44
+ // index.ts
45
+ import { createClient } from "contentful";
46
+ function createFieldContentful(contentType, options = {}) {
47
+ const {
48
+ space,
49
+ accessToken,
50
+ titleField = "title",
51
+ filterFields,
52
+ initialFilters
53
+ } = options;
54
+ if (!options.client) {
55
+ if (!space || !accessToken) {
56
+ throw new Error(
57
+ 'field-contentful: Must either specify "client", or "space" and "accessToken"'
58
+ );
59
+ }
60
+ }
61
+ const client = options.client || createClient({ space, accessToken });
62
+ const field = {
63
+ type: "external",
64
+ placeholder: "Select from Contentful",
65
+ showSearch: true,
66
+ fetchList: (_0) => __async(this, [_0], function* ({ query, filters = {} }) {
67
+ const entries = yield client.getEntries(__spreadProps(__spreadValues({}, filters), {
68
+ content_type: contentType,
69
+ query
70
+ }));
71
+ return entries.items;
72
+ }),
73
+ mapRow: ({ fields }) => fields,
74
+ getItemSummary: (item) => item.fields[titleField],
75
+ filterFields,
76
+ initialFilters
77
+ };
78
+ return field;
79
+ }
80
+ var field_contentful_default = createFieldContentful;
81
+ export {
82
+ createClient,
83
+ createFieldContentful,
84
+ field_contentful_default as default
85
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@measured/puck-field-contentful",
3
- "version": "0.16.0-canary.60fe631",
3
+ "version": "0.16.0-canary.6d43ba0",
4
4
  "author": "Measured Corporation Ltd <hello@measured.co>",
5
5
  "repository": "measuredco/puck",
6
6
  "bugs": "https://github.com/measuredco/puck/issues",
@@ -8,6 +8,10 @@
8
8
  "private": false,
9
9
  "main": "./dist/index.js",
10
10
  "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ "import": "./dist/index.mjs",
13
+ "types": "./dist/index.d.ts"
14
+ },
11
15
  "license": "MIT",
12
16
  "scripts": {
13
17
  "lint": "eslint \"**/*.ts*\"",
@@ -18,7 +22,7 @@
18
22
  "dist"
19
23
  ],
20
24
  "devDependencies": {
21
- "@measured/puck": "^0.16.0-canary.60fe631",
25
+ "@measured/puck": "^0.16.0-canary.6d43ba0",
22
26
  "@types/react": "^18.2.0",
23
27
  "@types/react-dom": "^18.2.0",
24
28
  "contentful": "^10.8.6",