@plasmicpkgs/plasmic-sanity-io 1.0.154 → 1.0.156
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/index.d.mts +52 -0
- package/dist/index.d.ts +52 -7
- package/dist/index.js +531 -5
- package/dist/index.mjs +496 -0
- package/package.json +12 -13
- package/dist/plasmic-sanity-io.cjs.development.js +0 -900
- package/dist/plasmic-sanity-io.cjs.development.js.map +0 -1
- package/dist/plasmic-sanity-io.cjs.production.min.js +0 -2
- package/dist/plasmic-sanity-io.cjs.production.min.js.map +0 -1
- package/dist/plasmic-sanity-io.esm.js +0 -886
- package/dist/plasmic-sanity-io.esm.js.map +0 -1
- package/dist/sanity.d.ts +0 -43
- package/dist/utils.d.ts +0 -4
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
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 SanityCredentialsProviderProps {
|
|
8
|
+
projectId?: string;
|
|
9
|
+
dataset?: string;
|
|
10
|
+
apiVersion?: string;
|
|
11
|
+
token?: string;
|
|
12
|
+
useCdn?: boolean;
|
|
13
|
+
}
|
|
14
|
+
declare const sanityCredentialsProviderMeta: GlobalContextMeta<SanityCredentialsProviderProps>;
|
|
15
|
+
declare function SanityCredentialsProvider({ projectId, dataset, apiVersion, token, useCdn, children, }: React.PropsWithChildren<SanityCredentialsProviderProps>): React.JSX.Element;
|
|
16
|
+
interface SanityFetcherProps {
|
|
17
|
+
groq?: string;
|
|
18
|
+
docType: string;
|
|
19
|
+
filterField?: string;
|
|
20
|
+
filterValue?: string;
|
|
21
|
+
filterParameter?: string;
|
|
22
|
+
noAutoRepeat?: boolean;
|
|
23
|
+
limit?: string;
|
|
24
|
+
children?: ReactNode;
|
|
25
|
+
className?: string;
|
|
26
|
+
noLayout?: boolean;
|
|
27
|
+
setControlContextData?: (data: {
|
|
28
|
+
docTypes?: string[];
|
|
29
|
+
sanityFields?: string[];
|
|
30
|
+
queryOptions?: [];
|
|
31
|
+
}) => void;
|
|
32
|
+
}
|
|
33
|
+
declare const sanityFetcherMeta: ComponentMeta<SanityFetcherProps>;
|
|
34
|
+
declare function SanityFetcher({ groq, docType, filterField, filterValue, filterParameter, limit, noAutoRepeat, children, className, noLayout, setControlContextData, }: SanityFetcherProps): React.JSX.Element;
|
|
35
|
+
interface SanityFieldProps {
|
|
36
|
+
className?: string;
|
|
37
|
+
path?: string;
|
|
38
|
+
field?: string;
|
|
39
|
+
setControlContextData?: (data: {
|
|
40
|
+
fields: string[];
|
|
41
|
+
isImage: boolean;
|
|
42
|
+
}) => void;
|
|
43
|
+
}
|
|
44
|
+
declare const sanityFieldMeta: ComponentMeta<SanityFieldProps>;
|
|
45
|
+
declare function SanityField({ className, path, field, setControlContextData, }: SanityFieldProps): React.JSX.Element;
|
|
46
|
+
|
|
47
|
+
declare function registerAll(loader?: {
|
|
48
|
+
registerComponent: typeof registerComponent;
|
|
49
|
+
registerGlobalContext: typeof registerGlobalContext;
|
|
50
|
+
}): void;
|
|
51
|
+
|
|
52
|
+
export { SanityCredentialsProvider, SanityFetcher, SanityField, ensure, registerAll, sanityCredentialsProviderMeta, sanityFetcherMeta, sanityFieldMeta };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,52 @@
|
|
|
1
|
-
import registerComponent from
|
|
2
|
-
import registerGlobalContext from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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 SanityCredentialsProviderProps {
|
|
8
|
+
projectId?: string;
|
|
9
|
+
dataset?: string;
|
|
10
|
+
apiVersion?: string;
|
|
11
|
+
token?: string;
|
|
12
|
+
useCdn?: boolean;
|
|
13
|
+
}
|
|
14
|
+
declare const sanityCredentialsProviderMeta: GlobalContextMeta<SanityCredentialsProviderProps>;
|
|
15
|
+
declare function SanityCredentialsProvider({ projectId, dataset, apiVersion, token, useCdn, children, }: React.PropsWithChildren<SanityCredentialsProviderProps>): React.JSX.Element;
|
|
16
|
+
interface SanityFetcherProps {
|
|
17
|
+
groq?: string;
|
|
18
|
+
docType: string;
|
|
19
|
+
filterField?: string;
|
|
20
|
+
filterValue?: string;
|
|
21
|
+
filterParameter?: string;
|
|
22
|
+
noAutoRepeat?: boolean;
|
|
23
|
+
limit?: string;
|
|
24
|
+
children?: ReactNode;
|
|
25
|
+
className?: string;
|
|
26
|
+
noLayout?: boolean;
|
|
27
|
+
setControlContextData?: (data: {
|
|
28
|
+
docTypes?: string[];
|
|
29
|
+
sanityFields?: string[];
|
|
30
|
+
queryOptions?: [];
|
|
31
|
+
}) => void;
|
|
32
|
+
}
|
|
33
|
+
declare const sanityFetcherMeta: ComponentMeta<SanityFetcherProps>;
|
|
34
|
+
declare function SanityFetcher({ groq, docType, filterField, filterValue, filterParameter, limit, noAutoRepeat, children, className, noLayout, setControlContextData, }: SanityFetcherProps): React.JSX.Element;
|
|
35
|
+
interface SanityFieldProps {
|
|
36
|
+
className?: string;
|
|
37
|
+
path?: string;
|
|
38
|
+
field?: string;
|
|
39
|
+
setControlContextData?: (data: {
|
|
40
|
+
fields: string[];
|
|
41
|
+
isImage: boolean;
|
|
42
|
+
}) => void;
|
|
43
|
+
}
|
|
44
|
+
declare const sanityFieldMeta: ComponentMeta<SanityFieldProps>;
|
|
45
|
+
declare function SanityField({ className, path, field, setControlContextData, }: SanityFieldProps): React.JSX.Element;
|
|
46
|
+
|
|
47
|
+
declare function registerAll(loader?: {
|
|
48
|
+
registerComponent: typeof registerComponent;
|
|
49
|
+
registerGlobalContext: typeof registerGlobalContext;
|
|
50
|
+
}): void;
|
|
51
|
+
|
|
52
|
+
export { SanityCredentialsProvider, SanityFetcher, SanityField, ensure, registerAll, sanityCredentialsProviderMeta, sanityFetcherMeta, sanityFieldMeta };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,534 @@
|
|
|
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
|
-
|
|
30
|
+
// src/index.tsx
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
SanityCredentialsProvider: () => SanityCredentialsProvider,
|
|
34
|
+
SanityFetcher: () => SanityFetcher,
|
|
35
|
+
SanityField: () => SanityField,
|
|
36
|
+
ensure: () => ensure,
|
|
37
|
+
registerAll: () => registerAll,
|
|
38
|
+
sanityCredentialsProviderMeta: () => sanityCredentialsProviderMeta,
|
|
39
|
+
sanityFetcherMeta: () => sanityFetcherMeta,
|
|
40
|
+
sanityFieldMeta: () => sanityFieldMeta
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
46
|
+
// src/sanity.tsx
|
|
47
|
+
var import_host = require("@plasmicapp/host");
|
|
48
|
+
var import_query = require("@plasmicapp/query");
|
|
49
|
+
var import_client = require("@sanity/client");
|
|
50
|
+
var import_image_url = __toESM(require("@sanity/image-url"));
|
|
51
|
+
var import_change_case = require("change-case");
|
|
52
|
+
var import_dlv = __toESM(require("dlv"));
|
|
53
|
+
var import_react = __toESM(require("react"));
|
|
54
|
+
|
|
55
|
+
// src/utils.ts
|
|
56
|
+
var filterParameters = [
|
|
57
|
+
{
|
|
58
|
+
value: "==",
|
|
59
|
+
label: "Is"
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
value: "!=",
|
|
63
|
+
label: "Is not"
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
value: ">",
|
|
67
|
+
label: "Greater than"
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
value: "<",
|
|
71
|
+
label: "Less than"
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
value: "<=",
|
|
75
|
+
label: "Less than or equal"
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
value: ">=",
|
|
79
|
+
label: "Greater than or equal "
|
|
80
|
+
}
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
// src/sanity.tsx
|
|
84
|
+
function ensure(x) {
|
|
85
|
+
if (x === null || x === void 0) {
|
|
86
|
+
throw new Error(`Value must not be undefined or null`);
|
|
87
|
+
} else {
|
|
88
|
+
return x;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
var modulePath = "@plasmicpkgs/plasmic-sanity-io";
|
|
92
|
+
var makeDataProviderName = (docType) => `currentSanity${(0, import_change_case.pascalCase)(docType)}Item`;
|
|
93
|
+
function makeSanityClient(creds) {
|
|
94
|
+
const sanity = (0, import_client.createClient)({
|
|
95
|
+
projectId: creds.projectId,
|
|
96
|
+
dataset: creds.dataset,
|
|
97
|
+
apiVersion: creds.apiVersion ? creds.apiVersion : "v1",
|
|
98
|
+
token: creds.token,
|
|
99
|
+
useCdn: creds.useCdn
|
|
100
|
+
});
|
|
101
|
+
return sanity;
|
|
102
|
+
}
|
|
103
|
+
var CredentialsContext = import_react.default.createContext(void 0);
|
|
104
|
+
var sanityCredentialsProviderMeta = {
|
|
105
|
+
name: "SanityCredentialsProvider",
|
|
106
|
+
displayName: "Sanity Credentials Provider",
|
|
107
|
+
description: `Get your project ID, dataset, and token [here](https://www.sanity.io/manage).
|
|
108
|
+
|
|
109
|
+
Add 'https://host.plasmicdev.com' (or your app host origin) as an authorized host in the CORS origins section of your Sanity project.
|
|
110
|
+
|
|
111
|
+
[See tutorial video](https://www.youtube.com/watch?v=dLeu7I4RsYg).`,
|
|
112
|
+
importName: "SanityCredentialsProvider",
|
|
113
|
+
importPath: modulePath,
|
|
114
|
+
props: {
|
|
115
|
+
projectId: {
|
|
116
|
+
type: "string",
|
|
117
|
+
displayName: "Project ID",
|
|
118
|
+
defaultValueHint: "b2gfz67v",
|
|
119
|
+
defaultValue: "b2gfz67v",
|
|
120
|
+
description: "The ID of the project to use."
|
|
121
|
+
},
|
|
122
|
+
dataset: {
|
|
123
|
+
type: "string",
|
|
124
|
+
displayName: "Dataset",
|
|
125
|
+
defaultValueHint: "production",
|
|
126
|
+
defaultValue: "production",
|
|
127
|
+
description: "The dataset to use."
|
|
128
|
+
},
|
|
129
|
+
apiVersion: {
|
|
130
|
+
type: "string",
|
|
131
|
+
displayName: "API Version",
|
|
132
|
+
defaultValueHint: "v1",
|
|
133
|
+
description: "The API version to use (if not set, 'v1' will be used) - see https://www.sanity.io/docs/js-client#specifying-api-version."
|
|
134
|
+
},
|
|
135
|
+
token: {
|
|
136
|
+
type: "string",
|
|
137
|
+
displayName: "Token",
|
|
138
|
+
description: "The token to use (or leave blank for unauthenticated usage) - you can create tokens in the API section of your project (i.e. https://www.sanity.io/manage/personal/project/PROJECT_ID/api#tokens)."
|
|
139
|
+
},
|
|
140
|
+
useCdn: {
|
|
141
|
+
type: "boolean",
|
|
142
|
+
displayName: "Use CDN?",
|
|
143
|
+
defaultValueHint: false,
|
|
144
|
+
description: "Whether you want to use CDN ('false' if you want to ensure fresh data)."
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
function SanityCredentialsProvider({
|
|
149
|
+
projectId,
|
|
150
|
+
dataset,
|
|
151
|
+
apiVersion,
|
|
152
|
+
token,
|
|
153
|
+
useCdn,
|
|
154
|
+
children
|
|
155
|
+
}) {
|
|
156
|
+
return /* @__PURE__ */ import_react.default.createElement(
|
|
157
|
+
CredentialsContext.Provider,
|
|
158
|
+
{
|
|
159
|
+
value: { projectId, dataset, apiVersion, token, useCdn }
|
|
160
|
+
},
|
|
161
|
+
children
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
var sanityFetcherMeta = {
|
|
165
|
+
name: "SanityFetcher",
|
|
166
|
+
displayName: "Sanity Fetcher",
|
|
167
|
+
importName: "SanityFetcher",
|
|
168
|
+
importPath: modulePath,
|
|
169
|
+
providesData: true,
|
|
170
|
+
description: `Fetches Sanity data of a given collection, and repeats \`children\` slot content for each row fetched.
|
|
171
|
+
|
|
172
|
+
[See tutorial video](https://www.youtube.com/watch?v=1SLoVY3hkQ4) and [GROQ cheat sheet](https://www.sanity.io/docs/query-cheat-sheet).`,
|
|
173
|
+
defaultStyles: {
|
|
174
|
+
display: "grid",
|
|
175
|
+
gridTemplateColumns: "1fr 1fr 1fr 1fr",
|
|
176
|
+
gridRowGap: "8px",
|
|
177
|
+
gridColumnGap: "8px",
|
|
178
|
+
padding: "8px",
|
|
179
|
+
maxWidth: "100%"
|
|
180
|
+
},
|
|
181
|
+
props: {
|
|
182
|
+
children: {
|
|
183
|
+
type: "slot",
|
|
184
|
+
defaultValue: {
|
|
185
|
+
type: "vbox",
|
|
186
|
+
styles: {
|
|
187
|
+
padding: "8px"
|
|
188
|
+
},
|
|
189
|
+
children: {
|
|
190
|
+
type: "component",
|
|
191
|
+
name: "SanityField"
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
groq: {
|
|
196
|
+
type: "string",
|
|
197
|
+
displayName: "GROQ",
|
|
198
|
+
description: "Query in GROQ.",
|
|
199
|
+
defaultValueHint: "*[_type == 'movie']",
|
|
200
|
+
// Hide this if there's no groq, AND there's docType, so we're in
|
|
201
|
+
// "docType" mode
|
|
202
|
+
hidden: (props) => !props.groq && !!props.docType
|
|
203
|
+
},
|
|
204
|
+
docType: {
|
|
205
|
+
type: "choice",
|
|
206
|
+
options: (props, ctx) => {
|
|
207
|
+
var _a;
|
|
208
|
+
return (_a = ctx == null ? void 0 : ctx.docTypes) != null ? _a : [];
|
|
209
|
+
},
|
|
210
|
+
displayName: "Document type",
|
|
211
|
+
description: "Document type to be queried (*[_type == DOC_TYPE] shortcut).",
|
|
212
|
+
// Hide this if groq is specified, as groq always takes precedence
|
|
213
|
+
hidden: (props) => !!props.groq
|
|
214
|
+
},
|
|
215
|
+
filterField: {
|
|
216
|
+
type: "choice",
|
|
217
|
+
displayName: "Filter field",
|
|
218
|
+
description: "Field (from Collection) to filter by",
|
|
219
|
+
options: (props, ctx) => {
|
|
220
|
+
var _a;
|
|
221
|
+
return (_a = ctx == null ? void 0 : ctx.sanityFields) != null ? _a : [];
|
|
222
|
+
},
|
|
223
|
+
// Hide this if there's groq (so we're just using groq), or if there's
|
|
224
|
+
// no docType selected yet
|
|
225
|
+
hidden: (props, ctx) => !!props.groq || !props.docType
|
|
226
|
+
},
|
|
227
|
+
filterParameter: {
|
|
228
|
+
type: "choice",
|
|
229
|
+
displayName: "Filter Operation",
|
|
230
|
+
description: "Filter Option to filter by. Read more (https://www.sanity.io/docs/groq-operators#3b7211e976f6)",
|
|
231
|
+
options: (props, ctx) => {
|
|
232
|
+
var _a;
|
|
233
|
+
return (_a = ctx == null ? void 0 : ctx.queryOptions) != null ? _a : [];
|
|
234
|
+
},
|
|
235
|
+
// Hide if in groq mode, or if no filter field is selected yet
|
|
236
|
+
hidden: (props, ctx) => !!props.groq || !props.filterField
|
|
237
|
+
},
|
|
238
|
+
filterValue: {
|
|
239
|
+
type: "string",
|
|
240
|
+
displayName: "Filter value",
|
|
241
|
+
description: "Value to filter by, should be of filter field type",
|
|
242
|
+
// Hide if in groq mode, or if no filter field is selected yet
|
|
243
|
+
hidden: (props, ctx) => !!props.groq || !props.filterField
|
|
244
|
+
},
|
|
245
|
+
limit: {
|
|
246
|
+
type: "string",
|
|
247
|
+
displayName: "Limit",
|
|
248
|
+
description: "Limit",
|
|
249
|
+
// Hide if in groq mode
|
|
250
|
+
hidden: (props) => !!props.groq || !props.docType
|
|
251
|
+
},
|
|
252
|
+
noAutoRepeat: {
|
|
253
|
+
type: "boolean",
|
|
254
|
+
displayName: "No auto-repeat",
|
|
255
|
+
description: "Do not automatically repeat children for every category.",
|
|
256
|
+
defaultValue: false
|
|
257
|
+
},
|
|
258
|
+
noLayout: {
|
|
259
|
+
type: "boolean",
|
|
260
|
+
displayName: "No layout",
|
|
261
|
+
description: "When set, Sanity Fetcher 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.",
|
|
262
|
+
defaultValue: false
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
function SanityFetcher({
|
|
267
|
+
groq,
|
|
268
|
+
docType,
|
|
269
|
+
filterField,
|
|
270
|
+
filterValue,
|
|
271
|
+
filterParameter,
|
|
272
|
+
limit,
|
|
273
|
+
noAutoRepeat,
|
|
274
|
+
children,
|
|
275
|
+
className,
|
|
276
|
+
noLayout,
|
|
277
|
+
setControlContextData
|
|
278
|
+
}) {
|
|
279
|
+
var _a;
|
|
280
|
+
const projectIdRegex = new RegExp(/^[-a-z0-9]+$/i);
|
|
281
|
+
const datasetRegex = new RegExp(
|
|
282
|
+
/^(~[a-z0-9]{1}[-\w]{0,63}|[a-z0-9]{1}[-\w]{0,63})$/
|
|
283
|
+
);
|
|
284
|
+
const dateRegex = new RegExp(/^\d{4}-\d{2}-\d{2}$/);
|
|
285
|
+
const creds = ensure((0, import_react.useContext)(CredentialsContext));
|
|
286
|
+
if (!creds.projectId || !projectIdRegex.test(creds.projectId)) {
|
|
287
|
+
return /* @__PURE__ */ import_react.default.createElement("div", { className }, "Please specify a valid projectId, it can only contain only a-z, 0-9 and dashes.");
|
|
288
|
+
} else if (!creds.dataset || !datasetRegex.test(creds.dataset)) {
|
|
289
|
+
return /* @__PURE__ */ import_react.default.createElement("div", { className }, "Please specify a valid dataset, they can only contain lowercase characters, numbers, underscores and dashes, and start with tilde, and be maximum 64 characters.");
|
|
290
|
+
} else if (creds.apiVersion) {
|
|
291
|
+
if (creds.apiVersion !== "v1" && creds.apiVersion !== "1" && creds.apiVersion !== "X") {
|
|
292
|
+
const date = new Date(creds.apiVersion);
|
|
293
|
+
if (!(dateRegex.test(creds.apiVersion) && date instanceof Date && date.getTime() > 0)) {
|
|
294
|
+
return /* @__PURE__ */ import_react.default.createElement("div", { className }, "Please specify a valid API version, expected `v1`, `1` or date in format `YYYY-MM-DD`.");
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
const filterUniqueDocTypes = (records) => records.map((record) => record._type).reduce((acc, type) => {
|
|
299
|
+
if (!acc.includes(type)) {
|
|
300
|
+
acc.push(type);
|
|
301
|
+
}
|
|
302
|
+
return acc;
|
|
303
|
+
}, []);
|
|
304
|
+
const allDataTypes = (0, import_query.usePlasmicQueryData)(
|
|
305
|
+
JSON.stringify(creds) + "/SANITY_DOCTYPES",
|
|
306
|
+
async () => {
|
|
307
|
+
const sanity2 = makeSanityClient(creds);
|
|
308
|
+
const resp = await sanity2.fetch("*{_type}").then(filterUniqueDocTypes);
|
|
309
|
+
return resp;
|
|
310
|
+
}
|
|
311
|
+
);
|
|
312
|
+
const docTypes = (_a = allDataTypes.data) != null ? _a : false;
|
|
313
|
+
const hasFilter = !!docType && !!filterField && !!filterParameter && !!filterValue;
|
|
314
|
+
const generateUnfilteredGroq = () => {
|
|
315
|
+
if (groq) {
|
|
316
|
+
console.log("ORIG GROQ", groq);
|
|
317
|
+
return groq;
|
|
318
|
+
} else if (docType) {
|
|
319
|
+
let query = `*[_type=='${docType}']`;
|
|
320
|
+
if (hasFilter) {
|
|
321
|
+
query += `[0...10]`;
|
|
322
|
+
} else if (limit) {
|
|
323
|
+
query += `[0...${limit}]`;
|
|
324
|
+
}
|
|
325
|
+
console.log("UNFILTERED GROQ", query);
|
|
326
|
+
return query;
|
|
327
|
+
} else {
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
const unfilteredQuery = generateUnfilteredGroq();
|
|
332
|
+
const sanity = makeSanityClient(creds);
|
|
333
|
+
const { data: unfilteredData } = (0, import_query.usePlasmicQueryData)(
|
|
334
|
+
unfilteredQuery ? JSON.stringify({ fullQuery: unfilteredQuery, creds }) : null,
|
|
335
|
+
async () => {
|
|
336
|
+
return sanity.fetch(unfilteredQuery);
|
|
337
|
+
}
|
|
338
|
+
);
|
|
339
|
+
const generateFilteredQuery = () => {
|
|
340
|
+
if (!hasFilter || !unfilteredData) {
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
const fieldValues = Object.values(unfilteredData).flatMap((model) => Array.isArray(model) ? model : [model]).map((item) => {
|
|
344
|
+
const field = Object.entries(item).find((el) => el[0] === filterField);
|
|
345
|
+
return field == null ? void 0 : field[1];
|
|
346
|
+
});
|
|
347
|
+
let query = `*[_type=='${docType}'`;
|
|
348
|
+
if (fieldValues.some((v) => typeof v === "string")) {
|
|
349
|
+
query = `${query} && ${filterField} ${filterParameter} "${filterValue}"`;
|
|
350
|
+
} else {
|
|
351
|
+
query = `${query} && ${filterField} ${filterParameter} ${filterValue}`;
|
|
352
|
+
}
|
|
353
|
+
if (limit) {
|
|
354
|
+
query = `${query}][0...${limit}]`;
|
|
355
|
+
} else {
|
|
356
|
+
query = `${query}]`;
|
|
357
|
+
}
|
|
358
|
+
console.log("FILTERED GROQ", query);
|
|
359
|
+
return query;
|
|
360
|
+
};
|
|
361
|
+
const filteredQuery = generateFilteredQuery();
|
|
362
|
+
const { data: filteredData } = (0, import_query.usePlasmicQueryData)(
|
|
363
|
+
filteredQuery ? JSON.stringify({ filteredQuery, creds }) : null,
|
|
364
|
+
async () => {
|
|
365
|
+
const resp = await sanity.fetch(filteredQuery);
|
|
366
|
+
return resp;
|
|
367
|
+
}
|
|
368
|
+
);
|
|
369
|
+
if (!docTypes) {
|
|
370
|
+
return /* @__PURE__ */ import_react.default.createElement("div", { className }, "Please configure the Sanity provider with a valid projectId, dataset, and token (if necessary). Don't forget to add 'https://host.plasmicdev.com' as an authorized host on the CORS origins section of your project.");
|
|
371
|
+
}
|
|
372
|
+
setControlContextData == null ? void 0 : setControlContextData({
|
|
373
|
+
docTypes
|
|
374
|
+
});
|
|
375
|
+
if (!groq && !docType) {
|
|
376
|
+
return /* @__PURE__ */ import_react.default.createElement("div", { className }, "Please specify a valid GROQ query or select a Document type.");
|
|
377
|
+
}
|
|
378
|
+
if (!unfilteredData) {
|
|
379
|
+
return /* @__PURE__ */ import_react.default.createElement("div", { className }, "Loading...");
|
|
380
|
+
}
|
|
381
|
+
let sanityFields = unfilteredData.map((item) => {
|
|
382
|
+
const fields = Object.keys(item).filter((field) => {
|
|
383
|
+
const value = (0, import_dlv.default)(item, field);
|
|
384
|
+
return typeof value !== "object" && value._type !== "image" && typeof value === "number" || typeof value === "string" && !value.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/);
|
|
385
|
+
});
|
|
386
|
+
return fields;
|
|
387
|
+
});
|
|
388
|
+
let operators;
|
|
389
|
+
const matchedFields = Object.values(unfilteredData).flatMap((model) => Array.isArray(model) ? model : [model]).map((item) => {
|
|
390
|
+
const fields = Object.entries(item).find((el) => el[0] === filterField);
|
|
391
|
+
return fields;
|
|
392
|
+
});
|
|
393
|
+
Object.values(matchedFields).map((model) => Array.isArray(model) ? model : [model]).map((item) => {
|
|
394
|
+
if (typeof item[1] === "number" && typeof item[1] !== "object") {
|
|
395
|
+
operators = filterParameters;
|
|
396
|
+
} else if (typeof item[1] !== "number" && typeof item[1] !== "object" && typeof item[1] === "string") {
|
|
397
|
+
operators = [
|
|
398
|
+
{
|
|
399
|
+
value: "==",
|
|
400
|
+
label: "Equals"
|
|
401
|
+
},
|
|
402
|
+
{
|
|
403
|
+
value: "!=",
|
|
404
|
+
label: "Not equals"
|
|
405
|
+
}
|
|
406
|
+
];
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
setControlContextData == null ? void 0 : setControlContextData({
|
|
410
|
+
queryOptions: operators,
|
|
411
|
+
docTypes,
|
|
412
|
+
sanityFields: sanityFields[0]
|
|
413
|
+
});
|
|
414
|
+
if (hasFilter) {
|
|
415
|
+
if (!filterParameter) {
|
|
416
|
+
return /* @__PURE__ */ import_react.default.createElement("div", { className }, "Please specify a filter operation");
|
|
417
|
+
}
|
|
418
|
+
if (!filterValue) {
|
|
419
|
+
return /* @__PURE__ */ import_react.default.createElement("div", { className }, "Please specify a filter value");
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
if (hasFilter && !filteredData) {
|
|
423
|
+
return /* @__PURE__ */ import_react.default.createElement("div", { className }, "Loading...");
|
|
424
|
+
}
|
|
425
|
+
const resultData = hasFilter ? filteredData : unfilteredData;
|
|
426
|
+
const imageBuilder = (0, import_image_url.default)(sanity);
|
|
427
|
+
const repElements = noAutoRepeat ? children : resultData.map((item, index) => {
|
|
428
|
+
Object.keys(item).forEach((field) => {
|
|
429
|
+
if (item[field]._type === "image") {
|
|
430
|
+
item[field].imgUrl = imageBuilder.image(item[field]).ignoreImageParams().toString();
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
return docType ? /* @__PURE__ */ import_react.default.createElement(
|
|
434
|
+
import_host.DataProvider,
|
|
435
|
+
{
|
|
436
|
+
key: item._id,
|
|
437
|
+
name: "sanityItem",
|
|
438
|
+
data: item,
|
|
439
|
+
hidden: true
|
|
440
|
+
},
|
|
441
|
+
/* @__PURE__ */ import_react.default.createElement(import_host.DataProvider, { name: makeDataProviderName(docType), data: item }, (0, import_host.repeatedElement)(index, children))
|
|
442
|
+
) : /* @__PURE__ */ import_react.default.createElement(import_host.DataProvider, { key: item._id, name: "sanityItem", data: item }, (0, import_host.repeatedElement)(index, children));
|
|
443
|
+
});
|
|
444
|
+
return /* @__PURE__ */ import_react.default.createElement(import_host.DataProvider, { name: "sanityItems", data: resultData }, noLayout ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, " ", repElements, " ") : /* @__PURE__ */ import_react.default.createElement("div", { className }, " ", repElements, " "));
|
|
445
|
+
}
|
|
446
|
+
var sanityFieldMeta = {
|
|
447
|
+
name: "SanityField",
|
|
448
|
+
displayName: "Sanity Field",
|
|
449
|
+
importName: "SanityField",
|
|
450
|
+
importPath: modulePath,
|
|
451
|
+
props: {
|
|
452
|
+
path: {
|
|
453
|
+
type: "string",
|
|
454
|
+
displayName: "Path",
|
|
455
|
+
description: "Field path - see https://www.sanity.io/docs/ids.",
|
|
456
|
+
defaultValueHint: "castMembers.0._key"
|
|
457
|
+
},
|
|
458
|
+
field: {
|
|
459
|
+
type: "choice",
|
|
460
|
+
options: (props, ctx) => {
|
|
461
|
+
var _a;
|
|
462
|
+
return (_a = ctx == null ? void 0 : ctx.fields) != null ? _a : [];
|
|
463
|
+
},
|
|
464
|
+
displayName: "Field",
|
|
465
|
+
description: "Field to be displayed."
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
function SanityField({
|
|
470
|
+
className,
|
|
471
|
+
path,
|
|
472
|
+
field,
|
|
473
|
+
setControlContextData
|
|
474
|
+
}) {
|
|
475
|
+
const item = (0, import_host.useSelector)("sanityItem");
|
|
476
|
+
if (!item) {
|
|
477
|
+
return /* @__PURE__ */ import_react.default.createElement("div", null, "SanityField must be used within a SanityFetcher");
|
|
478
|
+
}
|
|
479
|
+
const displayableFields = Object.keys(item).filter((field2) => {
|
|
480
|
+
const value = (0, import_dlv.default)(item, field2);
|
|
481
|
+
return typeof value !== "object" || value._type === "image";
|
|
482
|
+
});
|
|
483
|
+
setControlContextData == null ? void 0 : setControlContextData({
|
|
484
|
+
fields: displayableFields,
|
|
485
|
+
isImage: false
|
|
486
|
+
});
|
|
487
|
+
if (!path && !field) {
|
|
488
|
+
return /* @__PURE__ */ import_react.default.createElement("div", null, "Please specify a valid path or select a field.");
|
|
489
|
+
}
|
|
490
|
+
if (!path) {
|
|
491
|
+
path = field;
|
|
492
|
+
}
|
|
493
|
+
const data = (0, import_dlv.default)(item, path);
|
|
494
|
+
setControlContextData == null ? void 0 : setControlContextData({
|
|
495
|
+
fields: displayableFields,
|
|
496
|
+
isImage: (data == null ? void 0 : data._type) == "image"
|
|
497
|
+
});
|
|
498
|
+
if (!data) {
|
|
499
|
+
return /* @__PURE__ */ import_react.default.createElement("div", null, "Please specify a valid path.");
|
|
500
|
+
} else if ((data == null ? void 0 : data._type) === "image") {
|
|
501
|
+
return /* @__PURE__ */ import_react.default.createElement("img", { className, src: data.imgUrl });
|
|
502
|
+
} else {
|
|
503
|
+
return /* @__PURE__ */ import_react.default.createElement("div", { className }, data);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// src/index.tsx
|
|
508
|
+
function registerAll(loader) {
|
|
509
|
+
const _registerComponent = (Component, defaultMeta) => {
|
|
510
|
+
if (loader) {
|
|
511
|
+
loader.registerComponent(Component, defaultMeta);
|
|
512
|
+
} else {
|
|
513
|
+
(0, import_registerComponent.default)(Component, defaultMeta);
|
|
514
|
+
}
|
|
515
|
+
};
|
|
516
|
+
if (loader) {
|
|
517
|
+
loader.registerGlobalContext(SanityCredentialsProvider, sanityCredentialsProviderMeta);
|
|
518
|
+
} else {
|
|
519
|
+
(0, import_registerGlobalContext.default)(SanityCredentialsProvider, sanityCredentialsProviderMeta);
|
|
520
|
+
}
|
|
521
|
+
_registerComponent(SanityFetcher, sanityFetcherMeta);
|
|
522
|
+
_registerComponent(SanityField, sanityFieldMeta);
|
|
8
523
|
}
|
|
524
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
525
|
+
0 && (module.exports = {
|
|
526
|
+
SanityCredentialsProvider,
|
|
527
|
+
SanityFetcher,
|
|
528
|
+
SanityField,
|
|
529
|
+
ensure,
|
|
530
|
+
registerAll,
|
|
531
|
+
sanityCredentialsProviderMeta,
|
|
532
|
+
sanityFetcherMeta,
|
|
533
|
+
sanityFieldMeta
|
|
534
|
+
});
|