@hygraph/app-sdk 0.0.2
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/README.md +15 -0
- package/dist/app-sdk.cjs.development.js +167 -0
- package/dist/app-sdk.cjs.development.js.map +1 -0
- package/dist/app-sdk.cjs.production.min.js +2 -0
- package/dist/app-sdk.cjs.production.min.js.map +1 -0
- package/dist/app-sdk.esm.js +158 -0
- package/dist/app-sdk.esm.js.map +1 -0
- package/dist/app-sdk.umd.development.js +171 -0
- package/dist/app-sdk.umd.development.js.map +1 -0
- package/dist/app-sdk.umd.production.min.js +2 -0
- package/dist/app-sdk.umd.production.min.js.map +1 -0
- package/dist/app.d.ts +8 -0
- package/dist/base.d.ts +56 -0
- package/dist/field.d.ts +54 -0
- package/dist/formSidebar.d.ts +27 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.js +8 -0
- package/dist/type-helpers/appInstallation.d.ts +11 -0
- package/dist/type-helpers/dialog.d.ts +5 -0
- package/dist/type-helpers/field.d.ts +12 -0
- package/dist/type-helpers/form.d.ts +20 -0
- package/dist/type-helpers/locale.d.ts +9 -0
- package/dist/type-helpers/model.d.ts +8 -0
- package/dist/type-helpers/openAssetPicker.d.ts +13 -0
- package/dist/type-helpers/stage.d.ts +12 -0
- package/dist/type-helpers/toast.d.ts +14 -0
- package/dist/type-helpers/user.d.ts +8 -0
- package/dist/type-helpers/visibility.d.ts +5 -0
- package/package.json +41 -0
- package/src/app.ts +11 -0
- package/src/base.ts +93 -0
- package/src/declaration.d.ts +2 -0
- package/src/field.ts +73 -0
- package/src/formSidebar.ts +33 -0
- package/src/index.ts +177 -0
- package/src/type-helpers/appInstallation.ts +13 -0
- package/src/type-helpers/dialog.ts +11 -0
- package/src/type-helpers/field.ts +13 -0
- package/src/type-helpers/form.ts +44 -0
- package/src/type-helpers/locale.ts +10 -0
- package/src/type-helpers/model.ts +8 -0
- package/src/type-helpers/openAssetPicker.ts +16 -0
- package/src/type-helpers/stage.ts +21 -0
- package/src/type-helpers/toast.ts +30 -0
- package/src/type-helpers/user.ts +9 -0
- package/src/type-helpers/visibility.ts +7 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ExtensionPropsBase, ConfigValue } from './base';
|
|
2
|
+
import type { Form } from './type-helpers/form';
|
|
3
|
+
import type { Locale, FormLocale } from './type-helpers/locale';
|
|
4
|
+
import type { Model } from './type-helpers/model';
|
|
5
|
+
import type { Stage } from './type-helpers/stage';
|
|
6
|
+
import type { User } from './type-helpers/user';
|
|
7
|
+
import type { AppInstallation } from './type-helpers/appInstallation';
|
|
8
|
+
|
|
9
|
+
export interface FormSidebarExtensionProps extends ExtensionPropsBase {
|
|
10
|
+
// TODO:Add support for these
|
|
11
|
+
// isExpanded: boolean;
|
|
12
|
+
// expand: (expand: boolean | ((isExpanded: boolean) => boolean)) => unknown;
|
|
13
|
+
|
|
14
|
+
extension: {
|
|
15
|
+
config: ConfigValue;
|
|
16
|
+
sidebarConfig: ConfigValue;
|
|
17
|
+
id: string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
form: Form;
|
|
21
|
+
model: Model;
|
|
22
|
+
allLocales: Locale[];
|
|
23
|
+
selectedLocales: FormLocale[];
|
|
24
|
+
stages: Stage[];
|
|
25
|
+
entry: {
|
|
26
|
+
id: string | null;
|
|
27
|
+
createdBy?: User;
|
|
28
|
+
updatedBy?: User;
|
|
29
|
+
createdAt: Date | null;
|
|
30
|
+
updatedAt: Date | null;
|
|
31
|
+
} | null;
|
|
32
|
+
installation: AppInstallation;
|
|
33
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import zoid from '@graphcms/zoid/lib/zoid.js';
|
|
2
|
+
|
|
3
|
+
import { ExtensionDialogProps, reservedExtensionProps } from './base';
|
|
4
|
+
export { ExtensionDialogProps, reservedExtensionProps } from './base';
|
|
5
|
+
import type { FieldExtensionProps } from './field';
|
|
6
|
+
import type { FormSidebarExtensionProps } from './formSidebar';
|
|
7
|
+
export * from './base';
|
|
8
|
+
export type {
|
|
9
|
+
FormState,
|
|
10
|
+
FieldState,
|
|
11
|
+
FieldSubscription,
|
|
12
|
+
Subscriber,
|
|
13
|
+
FormSubscription,
|
|
14
|
+
Form,
|
|
15
|
+
} from './type-helpers/form';
|
|
16
|
+
|
|
17
|
+
export type {
|
|
18
|
+
VisibilityTypes,
|
|
19
|
+
SetFieldsVisibility,
|
|
20
|
+
VisibilityMap,
|
|
21
|
+
} from './type-helpers/visibility';
|
|
22
|
+
|
|
23
|
+
export type { FieldExtensionProps } from './field';
|
|
24
|
+
export type { AppProps } from './app';
|
|
25
|
+
export type { FormSidebarExtensionProps } from './formSidebar';
|
|
26
|
+
export { FieldExtensionFeature, FieldExtensionType } from './field';
|
|
27
|
+
export * from './type-helpers/appInstallation';
|
|
28
|
+
|
|
29
|
+
export type ExtensionProps =
|
|
30
|
+
| FieldExtensionProps
|
|
31
|
+
| FormSidebarExtensionProps
|
|
32
|
+
| ExtensionDialogProps;
|
|
33
|
+
|
|
34
|
+
type Xprops = {
|
|
35
|
+
onProps: (props: Record<string, any>) => void;
|
|
36
|
+
onConnected: (uid: string) => Promise<unknown>;
|
|
37
|
+
resize: (size: {
|
|
38
|
+
height: 'auto' | 'full' | number;
|
|
39
|
+
width: 'auto' | '100%' | number;
|
|
40
|
+
}) => unknown;
|
|
41
|
+
} & ExtensionProps;
|
|
42
|
+
|
|
43
|
+
declare global {
|
|
44
|
+
interface Window {
|
|
45
|
+
xprops: Xprops;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function handleProps(props: any) {
|
|
50
|
+
const transformedProps: any = {};
|
|
51
|
+
Object.keys(props).forEach((key: string) => {
|
|
52
|
+
// do not pass down zoid props to the extension
|
|
53
|
+
if (reservedExtensionProps.includes(key)) return;
|
|
54
|
+
|
|
55
|
+
// transform props that were prefixed with '_' to bypass zoid reverved props
|
|
56
|
+
if (
|
|
57
|
+
key.startsWith('_') &&
|
|
58
|
+
reservedExtensionProps.includes(key.replace(/^_/g, ''))
|
|
59
|
+
) {
|
|
60
|
+
transformedProps[key.replace(/^_/g, '')] = props[key];
|
|
61
|
+
} else {
|
|
62
|
+
transformedProps[key] = props[key];
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
return transformedProps;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function init({
|
|
69
|
+
debug,
|
|
70
|
+
onProps = () => undefined,
|
|
71
|
+
uid: givenUid,
|
|
72
|
+
}: {
|
|
73
|
+
onProps: (props: any) => unknown;
|
|
74
|
+
debug?: boolean;
|
|
75
|
+
uid?: string;
|
|
76
|
+
}) {
|
|
77
|
+
return new Promise<{ status: 'ok'; props: any } | { status: 'validation' }>(
|
|
78
|
+
(resolve, reject) => {
|
|
79
|
+
if (
|
|
80
|
+
typeof window === 'undefined' ||
|
|
81
|
+
typeof window.postMessage === 'undefined'
|
|
82
|
+
) {
|
|
83
|
+
return reject({
|
|
84
|
+
error: 'unsupported_env',
|
|
85
|
+
message:
|
|
86
|
+
'Unsupported environment: Not in a browser supporting PostMessage',
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const uid =
|
|
91
|
+
givenUid ||
|
|
92
|
+
(typeof URLSearchParams !== 'undefined' &&
|
|
93
|
+
new URLSearchParams(window.location.search).get('extensionUid'));
|
|
94
|
+
|
|
95
|
+
if (!uid) {
|
|
96
|
+
if (debug)
|
|
97
|
+
console.error(`[UIX] no uid found in init params or extension URL`);
|
|
98
|
+
return reject({
|
|
99
|
+
error: 'missing_uid',
|
|
100
|
+
message: 'Missing UID: no UID found in init params or extension URL',
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
if (debug) console.info(`[UIX:${uid}] initializing with uid ${uid}`);
|
|
104
|
+
|
|
105
|
+
zoid.create({
|
|
106
|
+
tag: uid,
|
|
107
|
+
url: window.location.href.toString(),
|
|
108
|
+
autoResize: {
|
|
109
|
+
width: false,
|
|
110
|
+
height: true,
|
|
111
|
+
element: 'html',
|
|
112
|
+
},
|
|
113
|
+
props: {
|
|
114
|
+
onConnected: {
|
|
115
|
+
type: 'function',
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
if (typeof window.xprops !== 'undefined') {
|
|
120
|
+
const { onConnected, onProps: initialOnProps } = window.xprops;
|
|
121
|
+
|
|
122
|
+
onConnected(uid).then((status) => {
|
|
123
|
+
if (status === true) {
|
|
124
|
+
const {
|
|
125
|
+
onProps: onParentProps,
|
|
126
|
+
onConnected: _nevermind,
|
|
127
|
+
resize,
|
|
128
|
+
...extensionProps
|
|
129
|
+
} = window.xprops;
|
|
130
|
+
|
|
131
|
+
if (debug)
|
|
132
|
+
console.info(`[UIX:${uid}] initial shared props`, extensionProps);
|
|
133
|
+
onParentProps((p: Xprops) => {
|
|
134
|
+
const {
|
|
135
|
+
onProps: onParentProps,
|
|
136
|
+
onConnected,
|
|
137
|
+
...newExtensionProps
|
|
138
|
+
} = p;
|
|
139
|
+
|
|
140
|
+
if (debug)
|
|
141
|
+
console.info(
|
|
142
|
+
`[UIX:${uid}] new shared props`,
|
|
143
|
+
newExtensionProps
|
|
144
|
+
);
|
|
145
|
+
if ('isExpanded' in p && typeof p.isExpanded === 'boolean') {
|
|
146
|
+
p.isExpanded
|
|
147
|
+
? resize({ height: 'full', width: '100%' })
|
|
148
|
+
: resize({ height: 'auto', width: '100%' });
|
|
149
|
+
}
|
|
150
|
+
onProps(handleProps(newExtensionProps));
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
if (debug) console.info(`[UIX:${uid}] initialized`);
|
|
154
|
+
onProps(handleProps(extensionProps));
|
|
155
|
+
resolve({ status: 'ok', props: handleProps(extensionProps) });
|
|
156
|
+
} else {
|
|
157
|
+
if (debug)
|
|
158
|
+
console.info(
|
|
159
|
+
`[UIX:${uid}] sdk renderer returned status:`,
|
|
160
|
+
status
|
|
161
|
+
);
|
|
162
|
+
resolve({ status: 'validation' });
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
} else {
|
|
166
|
+
if (debug) console.error(`[UIX] no shared props from host found`);
|
|
167
|
+
reject({
|
|
168
|
+
error: 'failed_communication',
|
|
169
|
+
message:
|
|
170
|
+
'No communication established with host, please check your URL',
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export default { init };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ConfigValue } from '../base';
|
|
2
|
+
|
|
3
|
+
export const AppInstallationStatus = {
|
|
4
|
+
COMPLETED: 'COMPLETED',
|
|
5
|
+
DISABLED: 'DISABLED',
|
|
6
|
+
PENDING: 'PENDING',
|
|
7
|
+
} as const;
|
|
8
|
+
|
|
9
|
+
export type AppInstallation = {
|
|
10
|
+
id: string;
|
|
11
|
+
config: ConfigValue;
|
|
12
|
+
status: keyof typeof AppInstallationStatus;
|
|
13
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { FieldExtensionType } from '../field';
|
|
2
|
+
|
|
3
|
+
export type Field = {
|
|
4
|
+
id: string;
|
|
5
|
+
apiId: string;
|
|
6
|
+
description: string | null;
|
|
7
|
+
displayName: string;
|
|
8
|
+
isList: boolean;
|
|
9
|
+
isLocalized: boolean;
|
|
10
|
+
isRequired: boolean;
|
|
11
|
+
isUnique: boolean;
|
|
12
|
+
type: keyof typeof FieldExtensionType;
|
|
13
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { SetFieldsVisibility } from '..';
|
|
2
|
+
import type {
|
|
3
|
+
FormSubscription,
|
|
4
|
+
FormState,
|
|
5
|
+
FieldState,
|
|
6
|
+
FieldSubscription,
|
|
7
|
+
FormSubscriber,
|
|
8
|
+
} from 'final-form';
|
|
9
|
+
|
|
10
|
+
export interface AnyObject {
|
|
11
|
+
[key: string]: any;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ValidationErrors extends AnyObject {}
|
|
15
|
+
export interface SubmissionErrors extends AnyObject {}
|
|
16
|
+
|
|
17
|
+
export type Subscriber<V = Record<string, any>> = (value: V) => void;
|
|
18
|
+
|
|
19
|
+
export type Form = {
|
|
20
|
+
change: <Value = any>(name: string, value: Value) => Promise<void>;
|
|
21
|
+
getState: <Values = Record<string, any>>() => Promise<FormState<Values>>;
|
|
22
|
+
getFieldState: <Value = any>(
|
|
23
|
+
fieldName: string
|
|
24
|
+
) => Promise<FieldState<Value> | undefined>;
|
|
25
|
+
subscribeToFieldState: <Value = any>(
|
|
26
|
+
name: string,
|
|
27
|
+
callback: (state: FieldState<Value>) => any,
|
|
28
|
+
subscription: FieldSubscription
|
|
29
|
+
) => Promise<() => any>;
|
|
30
|
+
subscribeToFormState: <Values = Record<string, any>>(
|
|
31
|
+
callback: FormSubscriber<Values>,
|
|
32
|
+
subscription: FormSubscription
|
|
33
|
+
) => Promise<() => any>;
|
|
34
|
+
setFieldsVisibility: SetFieldsVisibility;
|
|
35
|
+
changeBulk: (flatValues: Record<string, any>) => Promise<any>;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export {
|
|
39
|
+
FormSubscription,
|
|
40
|
+
FormState,
|
|
41
|
+
FieldState,
|
|
42
|
+
FieldSubscription,
|
|
43
|
+
FormSubscriber,
|
|
44
|
+
} from 'final-form';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type OpenAssetPicker = () => Promise<
|
|
2
|
+
null | (Asset & Record<string, unknown>)
|
|
3
|
+
>;
|
|
4
|
+
|
|
5
|
+
export interface Asset {
|
|
6
|
+
createdAt: string;
|
|
7
|
+
fileName: string;
|
|
8
|
+
handle: string;
|
|
9
|
+
height?: number;
|
|
10
|
+
id: string;
|
|
11
|
+
mimeType?: string;
|
|
12
|
+
size?: number;
|
|
13
|
+
updatedAt: string;
|
|
14
|
+
url: string;
|
|
15
|
+
width?: number;
|
|
16
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type ColorPalette =
|
|
2
|
+
| 'PINK'
|
|
3
|
+
| 'PURPLE'
|
|
4
|
+
| 'ORANGE'
|
|
5
|
+
| 'RED'
|
|
6
|
+
| 'BROWN'
|
|
7
|
+
| 'TEAL'
|
|
8
|
+
| 'GREEN'
|
|
9
|
+
| 'YELLOW';
|
|
10
|
+
|
|
11
|
+
export type Stage = {
|
|
12
|
+
id: string;
|
|
13
|
+
apiId: string;
|
|
14
|
+
color: string;
|
|
15
|
+
colorPaletteId: ColorPalette;
|
|
16
|
+
backgroundColor: string;
|
|
17
|
+
displayName: string;
|
|
18
|
+
description?: string | null | undefined;
|
|
19
|
+
isSystem: boolean;
|
|
20
|
+
position: number;
|
|
21
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type Id = number | string;
|
|
2
|
+
|
|
3
|
+
export type VariantColor =
|
|
4
|
+
| 'success'
|
|
5
|
+
| 'error'
|
|
6
|
+
| 'warning'
|
|
7
|
+
| 'info'
|
|
8
|
+
| 'primary'
|
|
9
|
+
| 'dark'
|
|
10
|
+
| 'publish';
|
|
11
|
+
|
|
12
|
+
export type Position =
|
|
13
|
+
| 'top-right'
|
|
14
|
+
| 'top-center'
|
|
15
|
+
| 'top-left'
|
|
16
|
+
| 'bottom-right'
|
|
17
|
+
| 'bottom-center'
|
|
18
|
+
| 'bottom-left';
|
|
19
|
+
|
|
20
|
+
type ToastOptions = {
|
|
21
|
+
title: string;
|
|
22
|
+
description?: string;
|
|
23
|
+
variantColor: VariantColor;
|
|
24
|
+
id?: Id;
|
|
25
|
+
isClosable?: boolean;
|
|
26
|
+
position?: Position;
|
|
27
|
+
duration?: number;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type ShowToast = (options: ToastOptions) => Promise<Id>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type VisibilityTypes = 'READ_WRITE' | 'READ_ONLY' | 'HIDDEN';
|
|
2
|
+
|
|
3
|
+
export type VisibilityMap = { [fieldApiId: string]: VisibilityTypes };
|
|
4
|
+
|
|
5
|
+
export type SetFieldsVisibility = (
|
|
6
|
+
arg: VisibilityMap | ((currentVisibilityMap: VisibilityMap) => VisibilityMap)
|
|
7
|
+
) => void;
|