@nixxie-cms/fields-oembed 1.0.1
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/LICENSE +23 -0
- package/README.md +16 -0
- package/dist/declarations/src/index.d.ts +31 -0
- package/dist/declarations/src/index.d.ts.map +1 -0
- package/dist/nixxie-cms-fields-oembed.cjs.d.ts +2 -0
- package/dist/nixxie-cms-fields-oembed.cjs.js +106 -0
- package/dist/nixxie-cms-fields-oembed.esm.js +101 -0
- package/package.json +33 -0
- package/src/index.ts +113 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nixxie International DMCC
|
|
4
|
+
Portions Copyright (c) 2023 Thinkmill Labs Pty Ltd and contributors
|
|
5
|
+
(this software is derived from the KeystoneJS project, https://keystonejs.com)
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# @nixxie-cms/fields-oembed
|
|
2
|
+
|
|
3
|
+
An oEmbed field for Nixxie CMS, for embeddable media URLs (YouTube, Vimeo, tweets, etc.). By
|
|
4
|
+
default it stores a validated URL string; set `resolve: true` to fetch and store the full oEmbed
|
|
5
|
+
payload as JSON on save.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { oembed } from '@nixxie-cms/fields-oembed'
|
|
9
|
+
|
|
10
|
+
fields: {
|
|
11
|
+
video: oembed(), // stores the validated URL
|
|
12
|
+
embed: oembed({ resolve: true }) // stores { url, html, title, thumbnailUrl, ... }
|
|
13
|
+
}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Also exports `fetchOembed(url, endpoint?)` for ad-hoc lookups.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { TextFieldConfig } from '@nixxie-cms/core/fields';
|
|
2
|
+
import type { BaseListTypeInfo, FieldTypeFunc } from '@nixxie-cms/core/types';
|
|
3
|
+
export type OembedData = {
|
|
4
|
+
url: string;
|
|
5
|
+
type?: string;
|
|
6
|
+
title?: string;
|
|
7
|
+
html?: string;
|
|
8
|
+
thumbnailUrl?: string;
|
|
9
|
+
providerName?: string;
|
|
10
|
+
raw?: any;
|
|
11
|
+
};
|
|
12
|
+
export type OembedFieldConfig<ListTypeInfo extends BaseListTypeInfo> = Omit<TextFieldConfig<ListTypeInfo>, 'validation'> & {
|
|
13
|
+
validation?: {
|
|
14
|
+
isRequired?: boolean;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* When set, the field stores the full resolved oEmbed payload (as JSON) instead of just the URL,
|
|
18
|
+
* fetching it on save from this oEmbed endpoint. Default endpoint: https://noembed.com/embed
|
|
19
|
+
*/
|
|
20
|
+
resolve?: boolean | {
|
|
21
|
+
endpoint: string;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/** Fetch oEmbed data for a URL via an oEmbed proxy endpoint (defaults to noembed.com). */
|
|
25
|
+
export declare function fetchOembed(url: string, endpoint?: string, timeoutMs?: number): Promise<OembedData>;
|
|
26
|
+
/**
|
|
27
|
+
* An oEmbed field for embeddable media URLs (YouTube, Vimeo, tweets, etc.). By default it stores a
|
|
28
|
+
* validated URL string. Set `resolve: true` to fetch and store the full oEmbed payload as JSON.
|
|
29
|
+
*/
|
|
30
|
+
export declare function oembed<ListTypeInfo extends BaseListTypeInfo>(config?: OembedFieldConfig<ListTypeInfo>): FieldTypeFunc<ListTypeInfo>;
|
|
31
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"../../../src","sources":["index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAmB,eAAe,EAAE,MAAM,yBAAyB,CAAA;AAC/E,OAAO,KAAK,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AAE7E,MAAM,MAAM,UAAU,GAAG;IACvB,GAAG,EAAE,MAAM,CAAA;IACX,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,GAAG,CAAC,EAAE,GAAG,CAAA;CACV,CAAA;AAED,MAAM,MAAM,iBAAiB,CAAC,YAAY,SAAS,gBAAgB,IAAI,IAAI,CACzE,eAAe,CAAC,YAAY,CAAC,EAC7B,YAAY,CACb,GAAG;IACF,UAAU,CAAC,EAAE;QAAE,UAAU,CAAC,EAAE,OAAO,CAAA;KAAE,CAAA;IACrC;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,GAAG;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAA;CACzC,CAAA;AAWD,0FAA0F;AAC1F,wBAAsB,WAAW,CAC/B,GAAG,EAAE,MAAM,EACX,QAAQ,SAA8B,EACtC,SAAS,SAAO,GACf,OAAO,CAAC,UAAU,CAAC,CAqBrB;AAED;;;GAGG;AACH,wBAAgB,MAAM,CAAC,YAAY,SAAS,gBAAgB,EAC1D,MAAM,GAAE,iBAAiB,CAAC,YAAY,CAAM,GAC3C,aAAa,CAAC,YAAY,CAAC,CA2C7B"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export * from "./declarations/src/index.js";
|
|
2
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibml4eGllLWNtcy1maWVsZHMtb2VtYmVkLmNqcy5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi9kZWNsYXJhdGlvbnMvc3JjL2luZGV4LmQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEifQ==
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var fields = require('@nixxie-cms/core/fields');
|
|
6
|
+
|
|
7
|
+
function isUrl(value) {
|
|
8
|
+
try {
|
|
9
|
+
const u = new URL(value);
|
|
10
|
+
return u.protocol === 'http:' || u.protocol === 'https:';
|
|
11
|
+
} catch {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Fetch oEmbed data for a URL via an oEmbed proxy endpoint (defaults to noembed.com). */
|
|
17
|
+
async function fetchOembed(url, endpoint = 'https://noembed.com/embed', timeoutMs = 5000) {
|
|
18
|
+
// Bound the request so a slow/hanging oEmbed provider can't stall the create/update mutation.
|
|
19
|
+
const controller = new AbortController();
|
|
20
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
21
|
+
let res;
|
|
22
|
+
try {
|
|
23
|
+
res = await fetch(`${endpoint}?url=${encodeURIComponent(url)}`, {
|
|
24
|
+
signal: controller.signal
|
|
25
|
+
});
|
|
26
|
+
} finally {
|
|
27
|
+
clearTimeout(timer);
|
|
28
|
+
}
|
|
29
|
+
if (!res.ok) throw new Error(`oEmbed lookup failed (${res.status})`);
|
|
30
|
+
const raw = await res.json();
|
|
31
|
+
return {
|
|
32
|
+
url,
|
|
33
|
+
type: raw.type,
|
|
34
|
+
title: raw.title,
|
|
35
|
+
html: raw.html,
|
|
36
|
+
thumbnailUrl: raw.thumbnail_url,
|
|
37
|
+
providerName: raw.provider_name,
|
|
38
|
+
raw
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* An oEmbed field for embeddable media URLs (YouTube, Vimeo, tweets, etc.). By default it stores a
|
|
44
|
+
* validated URL string. Set `resolve: true` to fetch and store the full oEmbed payload as JSON.
|
|
45
|
+
*/
|
|
46
|
+
function oembed(config = {}) {
|
|
47
|
+
const {
|
|
48
|
+
validation,
|
|
49
|
+
resolve,
|
|
50
|
+
hooks,
|
|
51
|
+
...rest
|
|
52
|
+
} = config;
|
|
53
|
+
const endpoint = resolve && typeof resolve === 'object' ? resolve.endpoint : 'https://noembed.com/embed';
|
|
54
|
+
if (resolve) {
|
|
55
|
+
return fields.json({
|
|
56
|
+
...rest,
|
|
57
|
+
defaultValue: null,
|
|
58
|
+
hooks: {
|
|
59
|
+
...hooks,
|
|
60
|
+
resolveInput: async args => {
|
|
61
|
+
const value = args.resolvedData[args.fieldKey];
|
|
62
|
+
if (value === undefined) return undefined;
|
|
63
|
+
if (value === null) return null;
|
|
64
|
+
const urlString = typeof value === 'string' ? value : value === null || value === void 0 ? void 0 : value.url;
|
|
65
|
+
if (!urlString || !isUrl(urlString)) return value;
|
|
66
|
+
try {
|
|
67
|
+
return await fetchOembed(urlString, endpoint);
|
|
68
|
+
} catch {
|
|
69
|
+
return {
|
|
70
|
+
url: urlString
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
return fields.text({
|
|
78
|
+
...rest,
|
|
79
|
+
validation: {
|
|
80
|
+
isRequired: validation === null || validation === void 0 ? void 0 : validation.isRequired
|
|
81
|
+
},
|
|
82
|
+
ui: {
|
|
83
|
+
description: 'An embeddable media URL',
|
|
84
|
+
...rest.ui
|
|
85
|
+
},
|
|
86
|
+
hooks: {
|
|
87
|
+
...hooks,
|
|
88
|
+
validate: args => {
|
|
89
|
+
const {
|
|
90
|
+
resolvedData,
|
|
91
|
+
fieldKey,
|
|
92
|
+
addValidationError,
|
|
93
|
+
operation
|
|
94
|
+
} = args;
|
|
95
|
+
if (operation === 'delete') return;
|
|
96
|
+
const value = resolvedData[fieldKey];
|
|
97
|
+
if (typeof value === 'string' && value.length > 0 && !isUrl(value)) {
|
|
98
|
+
addValidationError(`${fieldKey} must be a valid http(s) URL`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
exports.fetchOembed = fetchOembed;
|
|
106
|
+
exports.oembed = oembed;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { json, text } from '@nixxie-cms/core/fields';
|
|
2
|
+
|
|
3
|
+
function isUrl(value) {
|
|
4
|
+
try {
|
|
5
|
+
const u = new URL(value);
|
|
6
|
+
return u.protocol === 'http:' || u.protocol === 'https:';
|
|
7
|
+
} catch {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Fetch oEmbed data for a URL via an oEmbed proxy endpoint (defaults to noembed.com). */
|
|
13
|
+
async function fetchOembed(url, endpoint = 'https://noembed.com/embed', timeoutMs = 5000) {
|
|
14
|
+
// Bound the request so a slow/hanging oEmbed provider can't stall the create/update mutation.
|
|
15
|
+
const controller = new AbortController();
|
|
16
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
17
|
+
let res;
|
|
18
|
+
try {
|
|
19
|
+
res = await fetch(`${endpoint}?url=${encodeURIComponent(url)}`, {
|
|
20
|
+
signal: controller.signal
|
|
21
|
+
});
|
|
22
|
+
} finally {
|
|
23
|
+
clearTimeout(timer);
|
|
24
|
+
}
|
|
25
|
+
if (!res.ok) throw new Error(`oEmbed lookup failed (${res.status})`);
|
|
26
|
+
const raw = await res.json();
|
|
27
|
+
return {
|
|
28
|
+
url,
|
|
29
|
+
type: raw.type,
|
|
30
|
+
title: raw.title,
|
|
31
|
+
html: raw.html,
|
|
32
|
+
thumbnailUrl: raw.thumbnail_url,
|
|
33
|
+
providerName: raw.provider_name,
|
|
34
|
+
raw
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* An oEmbed field for embeddable media URLs (YouTube, Vimeo, tweets, etc.). By default it stores a
|
|
40
|
+
* validated URL string. Set `resolve: true` to fetch and store the full oEmbed payload as JSON.
|
|
41
|
+
*/
|
|
42
|
+
function oembed(config = {}) {
|
|
43
|
+
const {
|
|
44
|
+
validation,
|
|
45
|
+
resolve,
|
|
46
|
+
hooks,
|
|
47
|
+
...rest
|
|
48
|
+
} = config;
|
|
49
|
+
const endpoint = resolve && typeof resolve === 'object' ? resolve.endpoint : 'https://noembed.com/embed';
|
|
50
|
+
if (resolve) {
|
|
51
|
+
return json({
|
|
52
|
+
...rest,
|
|
53
|
+
defaultValue: null,
|
|
54
|
+
hooks: {
|
|
55
|
+
...hooks,
|
|
56
|
+
resolveInput: async args => {
|
|
57
|
+
const value = args.resolvedData[args.fieldKey];
|
|
58
|
+
if (value === undefined) return undefined;
|
|
59
|
+
if (value === null) return null;
|
|
60
|
+
const urlString = typeof value === 'string' ? value : value === null || value === void 0 ? void 0 : value.url;
|
|
61
|
+
if (!urlString || !isUrl(urlString)) return value;
|
|
62
|
+
try {
|
|
63
|
+
return await fetchOembed(urlString, endpoint);
|
|
64
|
+
} catch {
|
|
65
|
+
return {
|
|
66
|
+
url: urlString
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return text({
|
|
74
|
+
...rest,
|
|
75
|
+
validation: {
|
|
76
|
+
isRequired: validation === null || validation === void 0 ? void 0 : validation.isRequired
|
|
77
|
+
},
|
|
78
|
+
ui: {
|
|
79
|
+
description: 'An embeddable media URL',
|
|
80
|
+
...rest.ui
|
|
81
|
+
},
|
|
82
|
+
hooks: {
|
|
83
|
+
...hooks,
|
|
84
|
+
validate: args => {
|
|
85
|
+
const {
|
|
86
|
+
resolvedData,
|
|
87
|
+
fieldKey,
|
|
88
|
+
addValidationError,
|
|
89
|
+
operation
|
|
90
|
+
} = args;
|
|
91
|
+
if (operation === 'delete') return;
|
|
92
|
+
const value = resolvedData[fieldKey];
|
|
93
|
+
if (typeof value === 'string' && value.length > 0 && !isUrl(value)) {
|
|
94
|
+
addValidationError(`${fieldKey} must be a valid http(s) URL`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export { fetchOembed, oembed };
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nixxie-cms/fields-oembed",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"main": "dist/nixxie-cms-fields-oembed.cjs.js",
|
|
6
|
+
"module": "dist/nixxie-cms-fields-oembed.esm.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/nixxie-cms-fields-oembed.cjs.js",
|
|
10
|
+
"module": "./dist/nixxie-cms-fields-oembed.esm.js",
|
|
11
|
+
"default": "./dist/nixxie-cms-fields-oembed.cjs.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@babel/runtime": "^7.24.7"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@nixxie-cms/core": "^1.0.1"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@nixxie-cms/core": "^1.0.1"
|
|
23
|
+
},
|
|
24
|
+
"preconstruct": {
|
|
25
|
+
"entrypoints": [
|
|
26
|
+
"index.ts"
|
|
27
|
+
]
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/nixxiecms/nixxie/tree/main/packages/fields-oembed"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { json, text } from '@nixxie-cms/core/fields'
|
|
2
|
+
import type { JsonFieldConfig, TextFieldConfig } from '@nixxie-cms/core/fields'
|
|
3
|
+
import type { BaseListTypeInfo, FieldTypeFunc } from '@nixxie-cms/core/types'
|
|
4
|
+
|
|
5
|
+
export type OembedData = {
|
|
6
|
+
url: string
|
|
7
|
+
type?: string
|
|
8
|
+
title?: string
|
|
9
|
+
html?: string
|
|
10
|
+
thumbnailUrl?: string
|
|
11
|
+
providerName?: string
|
|
12
|
+
raw?: any
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type OembedFieldConfig<ListTypeInfo extends BaseListTypeInfo> = Omit<
|
|
16
|
+
TextFieldConfig<ListTypeInfo>,
|
|
17
|
+
'validation'
|
|
18
|
+
> & {
|
|
19
|
+
validation?: { isRequired?: boolean }
|
|
20
|
+
/**
|
|
21
|
+
* When set, the field stores the full resolved oEmbed payload (as JSON) instead of just the URL,
|
|
22
|
+
* fetching it on save from this oEmbed endpoint. Default endpoint: https://noembed.com/embed
|
|
23
|
+
*/
|
|
24
|
+
resolve?: boolean | { endpoint: string }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function isUrl(value: string): boolean {
|
|
28
|
+
try {
|
|
29
|
+
const u = new URL(value)
|
|
30
|
+
return u.protocol === 'http:' || u.protocol === 'https:'
|
|
31
|
+
} catch {
|
|
32
|
+
return false
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Fetch oEmbed data for a URL via an oEmbed proxy endpoint (defaults to noembed.com). */
|
|
37
|
+
export async function fetchOembed(
|
|
38
|
+
url: string,
|
|
39
|
+
endpoint = 'https://noembed.com/embed',
|
|
40
|
+
timeoutMs = 5000
|
|
41
|
+
): Promise<OembedData> {
|
|
42
|
+
// Bound the request so a slow/hanging oEmbed provider can't stall the create/update mutation.
|
|
43
|
+
const controller = new AbortController()
|
|
44
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs)
|
|
45
|
+
let res: Response
|
|
46
|
+
try {
|
|
47
|
+
res = await fetch(`${endpoint}?url=${encodeURIComponent(url)}`, { signal: controller.signal })
|
|
48
|
+
} finally {
|
|
49
|
+
clearTimeout(timer)
|
|
50
|
+
}
|
|
51
|
+
if (!res.ok) throw new Error(`oEmbed lookup failed (${res.status})`)
|
|
52
|
+
const raw: any = await res.json()
|
|
53
|
+
return {
|
|
54
|
+
url,
|
|
55
|
+
type: raw.type,
|
|
56
|
+
title: raw.title,
|
|
57
|
+
html: raw.html,
|
|
58
|
+
thumbnailUrl: raw.thumbnail_url,
|
|
59
|
+
providerName: raw.provider_name,
|
|
60
|
+
raw,
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* An oEmbed field for embeddable media URLs (YouTube, Vimeo, tweets, etc.). By default it stores a
|
|
66
|
+
* validated URL string. Set `resolve: true` to fetch and store the full oEmbed payload as JSON.
|
|
67
|
+
*/
|
|
68
|
+
export function oembed<ListTypeInfo extends BaseListTypeInfo>(
|
|
69
|
+
config: OembedFieldConfig<ListTypeInfo> = {}
|
|
70
|
+
): FieldTypeFunc<ListTypeInfo> {
|
|
71
|
+
const { validation, resolve, hooks, ...rest } = config
|
|
72
|
+
const endpoint =
|
|
73
|
+
resolve && typeof resolve === 'object' ? resolve.endpoint : 'https://noembed.com/embed'
|
|
74
|
+
|
|
75
|
+
if (resolve) {
|
|
76
|
+
return json<ListTypeInfo>({
|
|
77
|
+
...(rest as unknown as JsonFieldConfig<ListTypeInfo>),
|
|
78
|
+
defaultValue: null,
|
|
79
|
+
hooks: {
|
|
80
|
+
...hooks,
|
|
81
|
+
resolveInput: async (args: any) => {
|
|
82
|
+
const value = args.resolvedData[args.fieldKey]
|
|
83
|
+
if (value === undefined) return undefined
|
|
84
|
+
if (value === null) return null
|
|
85
|
+
const urlString = typeof value === 'string' ? value : value?.url
|
|
86
|
+
if (!urlString || !isUrl(urlString)) return value
|
|
87
|
+
try {
|
|
88
|
+
return await fetchOembed(urlString, endpoint)
|
|
89
|
+
} catch {
|
|
90
|
+
return { url: urlString }
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return text<ListTypeInfo>({
|
|
98
|
+
...(rest as TextFieldConfig<ListTypeInfo>),
|
|
99
|
+
validation: { isRequired: validation?.isRequired },
|
|
100
|
+
ui: { description: 'An embeddable media URL', ...rest.ui },
|
|
101
|
+
hooks: {
|
|
102
|
+
...hooks,
|
|
103
|
+
validate: (args: any) => {
|
|
104
|
+
const { resolvedData, fieldKey, addValidationError, operation } = args
|
|
105
|
+
if (operation === 'delete') return
|
|
106
|
+
const value = resolvedData[fieldKey]
|
|
107
|
+
if (typeof value === 'string' && value.length > 0 && !isUrl(value)) {
|
|
108
|
+
addValidationError(`${fieldKey} must be a valid http(s) URL`)
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
})
|
|
113
|
+
}
|