@anvilkit/plugin-asset-manager 0.1.6 → 0.1.8
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 +28 -20
- package/dist/adapters/data-url.d.cts +13 -0
- package/dist/adapters/data-url.d.cts.map +1 -1
- package/dist/adapters/data-url.d.ts +13 -0
- package/dist/adapters/data-url.d.ts.map +1 -1
- package/dist/adapters/in-memory.d.cts +8 -1
- package/dist/adapters/in-memory.d.cts.map +1 -1
- package/dist/adapters/in-memory.d.ts +8 -1
- package/dist/adapters/in-memory.d.ts.map +1 -1
- package/dist/i18n/entry.cjs +75 -0
- package/dist/i18n/entry.d.cts +15 -0
- package/dist/i18n/entry.d.cts.map +1 -0
- package/dist/i18n/entry.d.ts +15 -0
- package/dist/i18n/entry.d.ts.map +1 -0
- package/dist/i18n/entry.js +29 -0
- package/dist/i18n/provider.cjs +49 -0
- package/dist/i18n/provider.d.cts +20 -0
- package/dist/i18n/provider.d.cts.map +1 -0
- package/dist/i18n/provider.d.ts +20 -0
- package/dist/i18n/provider.d.ts.map +1 -0
- package/dist/i18n/provider.js +11 -0
- package/dist/index.cjs +7 -0
- package/dist/index.d.cts +11 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +11 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/plugin.cjs +70 -5
- package/dist/plugin.d.cts +3 -2
- package/dist/plugin.d.cts.map +1 -1
- package/dist/plugin.d.ts +3 -2
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +70 -5
- package/dist/sources/composite-source.cjs +138 -0
- package/dist/sources/composite-source.d.cts +39 -0
- package/dist/sources/composite-source.d.cts.map +1 -0
- package/dist/sources/composite-source.d.ts +39 -0
- package/dist/sources/composite-source.d.ts.map +1 -0
- package/dist/sources/composite-source.js +100 -0
- package/dist/sources/federated-search.cjs +173 -0
- package/dist/sources/federated-search.d.cts +33 -0
- package/dist/sources/federated-search.d.cts.map +1 -0
- package/dist/sources/federated-search.d.ts +33 -0
- package/dist/sources/federated-search.d.ts.map +1 -0
- package/dist/sources/federated-search.js +123 -0
- package/dist/sources/provider.cjs +18 -0
- package/dist/sources/provider.d.cts +51 -0
- package/dist/sources/provider.d.cts.map +1 -0
- package/dist/sources/provider.d.ts +51 -0
- package/dist/sources/provider.d.ts.map +1 -0
- package/dist/sources/provider.js +1 -0
- package/dist/sources/unsplash/client.cjs +189 -0
- package/dist/sources/unsplash/client.d.cts +87 -0
- package/dist/sources/unsplash/client.d.cts.map +1 -0
- package/dist/sources/unsplash/client.d.ts +87 -0
- package/dist/sources/unsplash/client.d.ts.map +1 -0
- package/dist/sources/unsplash/client.js +151 -0
- package/dist/sources/unsplash/index.cjs +194 -0
- package/dist/sources/unsplash/index.d.cts +16 -0
- package/dist/sources/unsplash/index.d.cts.map +1 -0
- package/dist/sources/unsplash/index.d.ts +16 -0
- package/dist/sources/unsplash/index.d.ts.map +1 -0
- package/dist/sources/unsplash/index.js +150 -0
- package/dist/sources/unsplash/themes.cjs +141 -0
- package/dist/sources/unsplash/themes.d.cts +18 -0
- package/dist/sources/unsplash/themes.d.cts.map +1 -0
- package/dist/sources/unsplash/themes.d.ts +18 -0
- package/dist/sources/unsplash/themes.d.ts.map +1 -0
- package/dist/sources/unsplash/themes.js +93 -0
- package/dist/sources/unsplash/throttle-cache.cjs +86 -0
- package/dist/sources/unsplash/throttle-cache.d.cts +25 -0
- package/dist/sources/unsplash/throttle-cache.d.cts.map +1 -0
- package/dist/sources/unsplash/throttle-cache.d.ts +25 -0
- package/dist/sources/unsplash/throttle-cache.d.ts.map +1 -0
- package/dist/sources/unsplash/throttle-cache.js +45 -0
- package/dist/types/categories.cjs +18 -0
- package/dist/types/categories.d.cts +48 -0
- package/dist/types/categories.d.cts.map +1 -0
- package/dist/types/categories.d.ts +48 -0
- package/dist/types/categories.d.ts.map +1 -0
- package/dist/types/categories.js +1 -0
- package/dist/types/data-source.cjs +18 -0
- package/dist/types/data-source.d.cts +59 -0
- package/dist/types/data-source.d.cts.map +1 -0
- package/dist/types/data-source.d.ts +59 -0
- package/dist/types/data-source.d.ts.map +1 -0
- package/dist/types/data-source.js +1 -0
- package/dist/types/filter.cjs +18 -0
- package/dist/types/filter.d.cts +55 -0
- package/dist/types/filter.d.cts.map +1 -0
- package/dist/types/filter.d.ts +55 -0
- package/dist/types/filter.d.ts.map +1 -0
- package/dist/types/filter.js +1 -0
- package/dist/types/folders.cjs +42 -0
- package/dist/types/folders.d.cts +46 -0
- package/dist/types/folders.d.cts.map +1 -0
- package/dist/types/folders.d.ts +46 -0
- package/dist/types/folders.d.ts.map +1 -0
- package/dist/types/folders.js +4 -0
- package/dist/types/options.cjs +18 -0
- package/dist/types/options.d.cts +68 -0
- package/dist/types/options.d.cts.map +1 -0
- package/dist/types/options.d.ts +68 -0
- package/dist/types/options.d.ts.map +1 -0
- package/dist/types/options.js +1 -0
- package/dist/types/types.d.cts +21 -27
- package/dist/types/types.d.cts.map +1 -1
- package/dist/types/types.d.ts +21 -27
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/unsplash.cjs +18 -0
- package/dist/types/unsplash.d.cts +60 -0
- package/dist/types/unsplash.d.cts.map +1 -0
- package/dist/types/unsplash.d.ts +60 -0
- package/dist/types/unsplash.d.ts.map +1 -0
- package/dist/types/unsplash.js +1 -0
- package/dist/ui/AssetBrowser.cjs +183 -121
- package/dist/ui/AssetBrowser.d.cts +12 -1
- package/dist/ui/AssetBrowser.d.cts.map +1 -1
- package/dist/ui/AssetBrowser.d.ts +12 -1
- package/dist/ui/AssetBrowser.d.ts.map +1 -1
- package/dist/ui/AssetBrowser.js +183 -121
- package/dist/ui/AssetCommandPalette.cjs +20 -18
- package/dist/ui/AssetCommandPalette.d.cts.map +1 -1
- package/dist/ui/AssetCommandPalette.d.ts.map +1 -1
- package/dist/ui/AssetCommandPalette.js +20 -18
- package/dist/ui/AssetManagerUI.cjs +31 -17
- package/dist/ui/AssetManagerUI.d.cts +19 -3
- package/dist/ui/AssetManagerUI.d.cts.map +1 -1
- package/dist/ui/AssetManagerUI.d.ts +19 -3
- package/dist/ui/AssetManagerUI.d.ts.map +1 -1
- package/dist/ui/AssetManagerUI.js +31 -17
- package/dist/ui/DeleteAssetDialog.cjs +5 -3
- package/dist/ui/DeleteAssetDialog.d.cts.map +1 -1
- package/dist/ui/DeleteAssetDialog.d.ts.map +1 -1
- package/dist/ui/DeleteAssetDialog.js +5 -3
- package/dist/ui/DeleteFolderDialog.cjs +80 -0
- package/dist/ui/DeleteFolderDialog.d.cts +11 -0
- package/dist/ui/DeleteFolderDialog.d.cts.map +1 -0
- package/dist/ui/DeleteFolderDialog.d.ts +11 -0
- package/dist/ui/DeleteFolderDialog.d.ts.map +1 -0
- package/dist/ui/DeleteFolderDialog.js +42 -0
- package/dist/ui/EmptyFolderState.cjs +56 -0
- package/dist/ui/EmptyFolderState.d.cts +6 -0
- package/dist/ui/EmptyFolderState.d.cts.map +1 -0
- package/dist/ui/EmptyFolderState.d.ts +6 -0
- package/dist/ui/EmptyFolderState.d.ts.map +1 -0
- package/dist/ui/EmptyFolderState.js +18 -0
- package/dist/ui/FolderBreadcrumb.cjs +76 -0
- package/dist/ui/FolderBreadcrumb.d.cts +9 -0
- package/dist/ui/FolderBreadcrumb.d.cts.map +1 -0
- package/dist/ui/FolderBreadcrumb.d.ts +9 -0
- package/dist/ui/FolderBreadcrumb.d.ts.map +1 -0
- package/dist/ui/FolderBreadcrumb.js +38 -0
- package/dist/ui/FolderNameDialog.cjs +105 -0
- package/dist/ui/FolderNameDialog.d.cts +14 -0
- package/dist/ui/FolderNameDialog.d.cts.map +1 -0
- package/dist/ui/FolderNameDialog.d.ts +14 -0
- package/dist/ui/FolderNameDialog.d.ts.map +1 -0
- package/dist/ui/FolderNameDialog.js +67 -0
- package/dist/ui/FolderTree.cjs +85 -0
- package/dist/ui/FolderTree.d.cts +13 -0
- package/dist/ui/FolderTree.d.cts.map +1 -0
- package/dist/ui/FolderTree.d.ts +13 -0
- package/dist/ui/FolderTree.d.ts.map +1 -0
- package/dist/ui/FolderTree.js +44 -0
- package/dist/ui/MetadataPanel.cjs +23 -17
- package/dist/ui/MetadataPanel.d.cts.map +1 -1
- package/dist/ui/MetadataPanel.d.ts.map +1 -1
- package/dist/ui/MetadataPanel.js +23 -17
- package/dist/ui/MoveTargetPicker.cjs +87 -0
- package/dist/ui/MoveTargetPicker.d.cts +16 -0
- package/dist/ui/MoveTargetPicker.d.cts.map +1 -0
- package/dist/ui/MoveTargetPicker.d.ts +16 -0
- package/dist/ui/MoveTargetPicker.d.ts.map +1 -0
- package/dist/ui/MoveTargetPicker.js +49 -0
- package/dist/ui/ReplaceAssetDialog.cjs +19 -17
- package/dist/ui/ReplaceAssetDialog.d.cts +2 -1
- package/dist/ui/ReplaceAssetDialog.d.cts.map +1 -1
- package/dist/ui/ReplaceAssetDialog.d.ts +2 -1
- package/dist/ui/ReplaceAssetDialog.d.ts.map +1 -1
- package/dist/ui/ReplaceAssetDialog.js +19 -17
- package/dist/ui/UnsplashPanel.cjs +137 -0
- package/dist/ui/UnsplashPanel.d.cts +28 -0
- package/dist/ui/UnsplashPanel.d.cts.map +1 -0
- package/dist/ui/UnsplashPanel.d.ts +28 -0
- package/dist/ui/UnsplashPanel.d.ts.map +1 -0
- package/dist/ui/UnsplashPanel.js +99 -0
- package/dist/ui/UploadButton.cjs +12 -13
- package/dist/ui/UploadButton.d.cts +9 -2
- package/dist/ui/UploadButton.d.cts.map +1 -1
- package/dist/ui/UploadButton.d.ts +9 -2
- package/dist/ui/UploadButton.d.ts.map +1 -1
- package/dist/ui/UploadButton.js +12 -13
- package/dist/ui/index.cjs +41 -0
- package/dist/ui/index.d.cts +15 -0
- package/dist/ui/index.d.cts.map +1 -1
- package/dist/ui/index.d.ts +15 -0
- package/dist/ui/index.d.ts.map +1 -1
- package/dist/ui/index.js +8 -0
- package/dist/utils/data-source.cjs +190 -0
- package/dist/utils/data-source.d.cts +63 -0
- package/dist/utils/data-source.d.cts.map +1 -0
- package/dist/utils/data-source.d.ts +63 -0
- package/dist/utils/data-source.d.ts.map +1 -0
- package/dist/utils/data-source.js +149 -0
- package/dist/utils/errors.cjs +18 -0
- package/dist/utils/errors.d.cts +27 -0
- package/dist/utils/errors.d.cts.map +1 -1
- package/dist/utils/errors.d.ts +27 -0
- package/dist/utils/errors.d.ts.map +1 -1
- package/dist/utils/errors.js +16 -1
- package/dist/utils/folders.cjs +335 -0
- package/dist/utils/folders.d.cts +54 -0
- package/dist/utils/folders.d.cts.map +1 -0
- package/dist/utils/folders.d.ts +54 -0
- package/dist/utils/folders.d.ts.map +1 -0
- package/dist/utils/folders.js +297 -0
- package/dist/utils/header-action.cjs +1 -1
- package/dist/utils/header-action.d.cts.map +1 -1
- package/dist/utils/header-action.d.ts.map +1 -1
- package/dist/utils/header-action.js +1 -1
- package/dist/utils/registry.cjs +38 -12
- package/dist/utils/registry.d.cts +29 -1
- package/dist/utils/registry.d.cts.map +1 -1
- package/dist/utils/registry.d.ts +29 -1
- package/dist/utils/registry.d.ts.map +1 -1
- package/dist/utils/registry.js +31 -11
- package/dist/utils/resolver.cjs +5 -4
- package/dist/utils/resolver.d.cts.map +1 -1
- package/dist/utils/resolver.d.ts.map +1 -1
- package/dist/utils/resolver.js +5 -4
- package/dist/utils/studio-asset-source.cjs +13 -4
- package/dist/utils/studio-asset-source.d.cts +6 -1
- package/dist/utils/studio-asset-source.d.cts.map +1 -1
- package/dist/utils/studio-asset-source.d.ts +6 -1
- package/dist/utils/studio-asset-source.d.ts.map +1 -1
- package/dist/utils/studio-asset-source.js +9 -3
- package/dist/utils/validate-upload-result.d.cts +2 -1
- package/dist/utils/validate-upload-result.d.cts.map +1 -1
- package/dist/utils/validate-upload-result.d.ts +2 -1
- package/dist/utils/validate-upload-result.d.ts.map +1 -1
- package/dist/version.cjs +1 -1
- package/dist/version.d.cts +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/i18n/messages/en.json +99 -0
- package/i18n/messages/ja.json +99 -0
- package/i18n/messages/ko.json +99 -0
- package/i18n/messages/zh.json +99 -0
- package/meta/config.json +1 -1
- package/package.json +19 -8
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
5
|
+
var define = (defs, kind)=>{
|
|
6
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
[kind]: defs[key]
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
define(getters, "get");
|
|
12
|
+
define(values, "value");
|
|
13
|
+
};
|
|
14
|
+
})();
|
|
15
|
+
(()=>{
|
|
16
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
17
|
+
})();
|
|
18
|
+
(()=>{
|
|
19
|
+
__webpack_require__.r = (exports1)=>{
|
|
20
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
21
|
+
value: 'Module'
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
24
|
+
value: true
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
28
|
+
var __webpack_exports__ = {};
|
|
29
|
+
__webpack_require__.r(__webpack_exports__);
|
|
30
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
+
createUnsplashClient: ()=>createUnsplashClient
|
|
32
|
+
});
|
|
33
|
+
const errors_cjs_namespaceObject = require("../../utils/errors.cjs");
|
|
34
|
+
function createTimedSignal(timeoutMs, caller) {
|
|
35
|
+
const controller = new AbortController();
|
|
36
|
+
let didTimeout = false;
|
|
37
|
+
const timer = setTimeout(()=>{
|
|
38
|
+
didTimeout = true;
|
|
39
|
+
controller.abort();
|
|
40
|
+
}, timeoutMs);
|
|
41
|
+
timer.unref?.();
|
|
42
|
+
const onCallerAbort = ()=>controller.abort();
|
|
43
|
+
if (caller) if (caller.aborted) controller.abort();
|
|
44
|
+
else caller.addEventListener("abort", onCallerAbort, {
|
|
45
|
+
once: true
|
|
46
|
+
});
|
|
47
|
+
return {
|
|
48
|
+
signal: controller.signal,
|
|
49
|
+
timedOut: ()=>didTimeout,
|
|
50
|
+
dispose: ()=>{
|
|
51
|
+
clearTimeout(timer);
|
|
52
|
+
caller?.removeEventListener("abort", onCallerAbort);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function isAbortError(error) {
|
|
57
|
+
return "object" == typeof error && null !== error && "AbortError" === error.name;
|
|
58
|
+
}
|
|
59
|
+
function parseRetryAfterMs(headers) {
|
|
60
|
+
const retryAfter = headers.get("retry-after");
|
|
61
|
+
if (null !== retryAfter) {
|
|
62
|
+
const seconds = Number(retryAfter);
|
|
63
|
+
if (Number.isFinite(seconds)) return 1000 * seconds;
|
|
64
|
+
}
|
|
65
|
+
const reset = headers.get("x-ratelimit-reset");
|
|
66
|
+
if (null !== reset) {
|
|
67
|
+
const epochSeconds = Number(reset);
|
|
68
|
+
if (Number.isFinite(epochSeconds)) {
|
|
69
|
+
const ms = 1000 * epochSeconds - Date.now();
|
|
70
|
+
if (ms > 0) return ms;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function mapHttpError(response) {
|
|
75
|
+
if (429 === response.status) {
|
|
76
|
+
const retryAfterMs = parseRetryAfterMs(response.headers);
|
|
77
|
+
return new errors_cjs_namespaceObject.AssetSourceError("PROVIDER_RATE_LIMITED", "Unsplash rate limit reached.", {
|
|
78
|
+
retryable: true,
|
|
79
|
+
status: 429,
|
|
80
|
+
...void 0 !== retryAfterMs ? {
|
|
81
|
+
retryAfterMs
|
|
82
|
+
} : {}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
if (401 === response.status || 403 === response.status) return new errors_cjs_namespaceObject.AssetSourceError("PROVIDER_UNAUTHORIZED", "Unsplash rejected the request — check the access key / proxy.", {
|
|
86
|
+
status: response.status
|
|
87
|
+
});
|
|
88
|
+
return new errors_cjs_namespaceObject.AssetSourceError("PROVIDER_BAD_RESPONSE", `Unsplash returned HTTP ${response.status}.`, {
|
|
89
|
+
status: response.status,
|
|
90
|
+
retryable: response.status >= 500
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
function createUnsplashClient(options) {
|
|
94
|
+
const doFetch = options.fetch ?? globalThis.fetch;
|
|
95
|
+
const usingProxy = void 0 !== options.proxyEndpoint;
|
|
96
|
+
const base = (usingProxy ? String(options.proxyEndpoint) : "https://api.unsplash.com").replace(/\/$/, "");
|
|
97
|
+
const timeoutMs = options.timeoutMs ?? 15000;
|
|
98
|
+
const trackDownloadTimeoutMs = options.trackDownloadTimeoutMs ?? 6000;
|
|
99
|
+
const authHeaders = ()=>!usingProxy && options.accessKey ? {
|
|
100
|
+
Authorization: `Client-ID ${options.accessKey}`
|
|
101
|
+
} : {};
|
|
102
|
+
const readJson = async (response)=>{
|
|
103
|
+
try {
|
|
104
|
+
return await response.json();
|
|
105
|
+
} catch (cause) {
|
|
106
|
+
throw new errors_cjs_namespaceObject.AssetSourceError("PROVIDER_BAD_RESPONSE", "Unsplash returned an unreadable response.", {
|
|
107
|
+
cause
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
const requestJson = async (path, params, signal)=>{
|
|
112
|
+
const search = new URLSearchParams();
|
|
113
|
+
for (const [key, value] of Object.entries(params))if (void 0 !== value) search.set(key, String(value));
|
|
114
|
+
const query = search.toString();
|
|
115
|
+
const url = `${base}${path}${query ? `?${query}` : ""}`;
|
|
116
|
+
const timed = createTimedSignal(timeoutMs, signal);
|
|
117
|
+
try {
|
|
118
|
+
let response;
|
|
119
|
+
try {
|
|
120
|
+
response = await doFetch(url, {
|
|
121
|
+
headers: authHeaders(),
|
|
122
|
+
signal: timed.signal
|
|
123
|
+
});
|
|
124
|
+
} catch (cause) {
|
|
125
|
+
if (timed.timedOut()) throw new errors_cjs_namespaceObject.AssetSourceError("PROVIDER_NETWORK", `Unsplash request timed out after ${timeoutMs} ms.`, {
|
|
126
|
+
retryable: true,
|
|
127
|
+
cause
|
|
128
|
+
});
|
|
129
|
+
if (isAbortError(cause)) throw cause;
|
|
130
|
+
throw new errors_cjs_namespaceObject.AssetSourceError("PROVIDER_NETWORK", "Network error contacting Unsplash.", {
|
|
131
|
+
retryable: true,
|
|
132
|
+
cause
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
if (!response.ok) throw mapHttpError(response);
|
|
136
|
+
return await readJson(response);
|
|
137
|
+
} finally{
|
|
138
|
+
timed.dispose();
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
return {
|
|
142
|
+
async searchPhotos (params, signal) {
|
|
143
|
+
const body = await requestJson("/search/photos", {
|
|
144
|
+
query: params.query,
|
|
145
|
+
page: params.page,
|
|
146
|
+
per_page: params.perPage,
|
|
147
|
+
orientation: params.orientation,
|
|
148
|
+
content_filter: params.contentFilter
|
|
149
|
+
}, signal);
|
|
150
|
+
return {
|
|
151
|
+
total: body.total ?? 0,
|
|
152
|
+
results: body.results ?? []
|
|
153
|
+
};
|
|
154
|
+
},
|
|
155
|
+
async topicPhotos (slug, params, signal) {
|
|
156
|
+
return await requestJson(`/topics/${encodeURIComponent(slug)}/photos`, {
|
|
157
|
+
page: params.page,
|
|
158
|
+
per_page: params.perPage,
|
|
159
|
+
orientation: params.orientation
|
|
160
|
+
}, signal) ?? [];
|
|
161
|
+
},
|
|
162
|
+
async listTopics (signal) {
|
|
163
|
+
return await requestJson("/topics", {
|
|
164
|
+
per_page: 30
|
|
165
|
+
}, signal) ?? [];
|
|
166
|
+
},
|
|
167
|
+
async getPhoto (id, signal) {
|
|
168
|
+
return requestJson(`/photos/${encodeURIComponent(id)}`, {}, signal);
|
|
169
|
+
},
|
|
170
|
+
async trackDownload (downloadLocation, signal) {
|
|
171
|
+
const timed = createTimedSignal(trackDownloadTimeoutMs, signal);
|
|
172
|
+
try {
|
|
173
|
+
await doFetch(downloadLocation, {
|
|
174
|
+
headers: authHeaders(),
|
|
175
|
+
signal: timed.signal
|
|
176
|
+
});
|
|
177
|
+
} catch {} finally{
|
|
178
|
+
timed.dispose();
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
exports.createUnsplashClient = __webpack_exports__.createUnsplashClient;
|
|
184
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
185
|
+
"createUnsplashClient"
|
|
186
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
187
|
+
Object.defineProperty(exports, '__esModule', {
|
|
188
|
+
value: true
|
|
189
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Dependency-free Unsplash REST client (PRD 0002 §8.6). Uses only
|
|
3
|
+
* `globalThis.fetch` — no `unsplash-js`, no new dependency or peer (which would
|
|
4
|
+
* fail the repo's `check:peer-deps` gate). Lazy-loaded, so it never enters the
|
|
5
|
+
* headless entry chunk.
|
|
6
|
+
*
|
|
7
|
+
* Base URL is the host's server-side proxy when supplied (which injects the
|
|
8
|
+
* `Client-ID`), else `https://api.unsplash.com` with a `Client-ID` header.
|
|
9
|
+
*/
|
|
10
|
+
export interface UnsplashClientOptions {
|
|
11
|
+
readonly proxyEndpoint?: string | URL;
|
|
12
|
+
readonly accessKey?: string;
|
|
13
|
+
readonly fetch?: typeof globalThis.fetch;
|
|
14
|
+
/**
|
|
15
|
+
* Per-request ceiling for search / topic / photo lookups (ms). A request
|
|
16
|
+
* that exceeds it aborts and surfaces as a retryable `PROVIDER_NETWORK`
|
|
17
|
+
* error instead of hanging on the platform's default socket timeout — so a
|
|
18
|
+
* blocked or flaky network path (e.g. a VPN/proxy black-holing
|
|
19
|
+
* `api.unsplash.com`) lets the sidebar fall back to a clean "unavailable"
|
|
20
|
+
* state. Default 15000. When the proxy route is used it has its own,
|
|
21
|
+
* tighter timeout + retry, so this is mainly a backstop for the direct path.
|
|
22
|
+
*/
|
|
23
|
+
readonly timeoutMs?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Ceiling for the best-effort, fire-and-forget download trigger (ms). It
|
|
26
|
+
* pings the absolute `download_location` URL DIRECTLY (never through the
|
|
27
|
+
* host proxy), so without a bound a stalled connection would leak a hanging
|
|
28
|
+
* request per insert. Default 6000.
|
|
29
|
+
*/
|
|
30
|
+
readonly trackDownloadTimeoutMs?: number;
|
|
31
|
+
}
|
|
32
|
+
export interface UnsplashPhotoUrls {
|
|
33
|
+
readonly raw?: string;
|
|
34
|
+
readonly full?: string;
|
|
35
|
+
readonly regular: string;
|
|
36
|
+
readonly small: string;
|
|
37
|
+
readonly thumb: string;
|
|
38
|
+
}
|
|
39
|
+
export interface UnsplashPhoto {
|
|
40
|
+
readonly id: string;
|
|
41
|
+
readonly width?: number;
|
|
42
|
+
readonly height?: number;
|
|
43
|
+
readonly description?: string | null;
|
|
44
|
+
readonly alt_description?: string | null;
|
|
45
|
+
readonly urls: UnsplashPhotoUrls;
|
|
46
|
+
readonly links: {
|
|
47
|
+
readonly html: string;
|
|
48
|
+
readonly download_location: string;
|
|
49
|
+
};
|
|
50
|
+
readonly user: {
|
|
51
|
+
readonly name: string;
|
|
52
|
+
readonly links: {
|
|
53
|
+
readonly html: string;
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
export interface UnsplashSearchResult {
|
|
58
|
+
readonly total: number;
|
|
59
|
+
readonly results: readonly UnsplashPhoto[];
|
|
60
|
+
}
|
|
61
|
+
export interface UnsplashTopicSummary {
|
|
62
|
+
readonly id: string;
|
|
63
|
+
readonly slug: string;
|
|
64
|
+
readonly title: string;
|
|
65
|
+
}
|
|
66
|
+
export interface UnsplashSearchParams {
|
|
67
|
+
readonly query: string;
|
|
68
|
+
readonly page?: number;
|
|
69
|
+
readonly perPage?: number;
|
|
70
|
+
readonly orientation?: string;
|
|
71
|
+
readonly contentFilter?: string;
|
|
72
|
+
}
|
|
73
|
+
export interface UnsplashClient {
|
|
74
|
+
searchPhotos(params: UnsplashSearchParams, signal?: AbortSignal): Promise<UnsplashSearchResult>;
|
|
75
|
+
topicPhotos(slug: string, params: {
|
|
76
|
+
page?: number;
|
|
77
|
+
perPage?: number;
|
|
78
|
+
orientation?: string;
|
|
79
|
+
}, signal?: AbortSignal): Promise<readonly UnsplashPhoto[]>;
|
|
80
|
+
listTopics(signal?: AbortSignal): Promise<readonly UnsplashTopicSummary[]>;
|
|
81
|
+
/** Fetch a single photo by id — used to recover `download_location` on a cache miss. */
|
|
82
|
+
getPhoto(id: string, signal?: AbortSignal): Promise<UnsplashPhoto>;
|
|
83
|
+
/** Fires the MANDATORY download trigger. Non-throwing — best-effort. */
|
|
84
|
+
trackDownload(downloadLocation: string, signal?: AbortSignal): Promise<void>;
|
|
85
|
+
}
|
|
86
|
+
export declare function createUnsplashClient(options: UnsplashClientOptions): UnsplashClient;
|
|
87
|
+
//# sourceMappingURL=client.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.cts","sourceRoot":"","sources":["../../../src/sources/unsplash/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,MAAM,WAAW,qBAAqB;IACrC,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;IACtC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;IACzC;;;;;;;;OAQG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B;;;;;OAKG;IACH,QAAQ,CAAC,sBAAsB,CAAC,EAAE,MAAM,CAAC;CACzC;AA4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Q,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;CAChC;AAED,MAAM,WAAW,cAAc;IAC9B,YAAY,CACX,MAAM,EAAE,oBAAoB,EAC5B,MAAM,CAAC,EAAE,WAAW,GAClB,OAAO,CAAC,oBAAoB,CAAC,CAAC;IACjC,WAAW,CACV,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE,EACjE,MAAM,CAAC,EAAE,WAAW,GAClB,OAAO,CAAC,SAAS,aAAa,EAAE,CAAC,CAAC;IACrC,UAAU,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,oBAAoB,EAAE,CAAC,CAAC;IAC3E,wFAAwF;IACxF,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IACnE,wEAAwE;IACxE,aAAa,CAAC,gBAAgB,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7E;AAsDD,wBAAgB,oBAAoB,CACnC,OAAO,EAAE,qBAAqB,GAC5B,cAAc,CAiJhB"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Dependency-free Unsplash REST client (PRD 0002 §8.6). Uses only
|
|
3
|
+
* `globalThis.fetch` — no `unsplash-js`, no new dependency or peer (which would
|
|
4
|
+
* fail the repo's `check:peer-deps` gate). Lazy-loaded, so it never enters the
|
|
5
|
+
* headless entry chunk.
|
|
6
|
+
*
|
|
7
|
+
* Base URL is the host's server-side proxy when supplied (which injects the
|
|
8
|
+
* `Client-ID`), else `https://api.unsplash.com` with a `Client-ID` header.
|
|
9
|
+
*/
|
|
10
|
+
export interface UnsplashClientOptions {
|
|
11
|
+
readonly proxyEndpoint?: string | URL;
|
|
12
|
+
readonly accessKey?: string;
|
|
13
|
+
readonly fetch?: typeof globalThis.fetch;
|
|
14
|
+
/**
|
|
15
|
+
* Per-request ceiling for search / topic / photo lookups (ms). A request
|
|
16
|
+
* that exceeds it aborts and surfaces as a retryable `PROVIDER_NETWORK`
|
|
17
|
+
* error instead of hanging on the platform's default socket timeout — so a
|
|
18
|
+
* blocked or flaky network path (e.g. a VPN/proxy black-holing
|
|
19
|
+
* `api.unsplash.com`) lets the sidebar fall back to a clean "unavailable"
|
|
20
|
+
* state. Default 15000. When the proxy route is used it has its own,
|
|
21
|
+
* tighter timeout + retry, so this is mainly a backstop for the direct path.
|
|
22
|
+
*/
|
|
23
|
+
readonly timeoutMs?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Ceiling for the best-effort, fire-and-forget download trigger (ms). It
|
|
26
|
+
* pings the absolute `download_location` URL DIRECTLY (never through the
|
|
27
|
+
* host proxy), so without a bound a stalled connection would leak a hanging
|
|
28
|
+
* request per insert. Default 6000.
|
|
29
|
+
*/
|
|
30
|
+
readonly trackDownloadTimeoutMs?: number;
|
|
31
|
+
}
|
|
32
|
+
export interface UnsplashPhotoUrls {
|
|
33
|
+
readonly raw?: string;
|
|
34
|
+
readonly full?: string;
|
|
35
|
+
readonly regular: string;
|
|
36
|
+
readonly small: string;
|
|
37
|
+
readonly thumb: string;
|
|
38
|
+
}
|
|
39
|
+
export interface UnsplashPhoto {
|
|
40
|
+
readonly id: string;
|
|
41
|
+
readonly width?: number;
|
|
42
|
+
readonly height?: number;
|
|
43
|
+
readonly description?: string | null;
|
|
44
|
+
readonly alt_description?: string | null;
|
|
45
|
+
readonly urls: UnsplashPhotoUrls;
|
|
46
|
+
readonly links: {
|
|
47
|
+
readonly html: string;
|
|
48
|
+
readonly download_location: string;
|
|
49
|
+
};
|
|
50
|
+
readonly user: {
|
|
51
|
+
readonly name: string;
|
|
52
|
+
readonly links: {
|
|
53
|
+
readonly html: string;
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
export interface UnsplashSearchResult {
|
|
58
|
+
readonly total: number;
|
|
59
|
+
readonly results: readonly UnsplashPhoto[];
|
|
60
|
+
}
|
|
61
|
+
export interface UnsplashTopicSummary {
|
|
62
|
+
readonly id: string;
|
|
63
|
+
readonly slug: string;
|
|
64
|
+
readonly title: string;
|
|
65
|
+
}
|
|
66
|
+
export interface UnsplashSearchParams {
|
|
67
|
+
readonly query: string;
|
|
68
|
+
readonly page?: number;
|
|
69
|
+
readonly perPage?: number;
|
|
70
|
+
readonly orientation?: string;
|
|
71
|
+
readonly contentFilter?: string;
|
|
72
|
+
}
|
|
73
|
+
export interface UnsplashClient {
|
|
74
|
+
searchPhotos(params: UnsplashSearchParams, signal?: AbortSignal): Promise<UnsplashSearchResult>;
|
|
75
|
+
topicPhotos(slug: string, params: {
|
|
76
|
+
page?: number;
|
|
77
|
+
perPage?: number;
|
|
78
|
+
orientation?: string;
|
|
79
|
+
}, signal?: AbortSignal): Promise<readonly UnsplashPhoto[]>;
|
|
80
|
+
listTopics(signal?: AbortSignal): Promise<readonly UnsplashTopicSummary[]>;
|
|
81
|
+
/** Fetch a single photo by id — used to recover `download_location` on a cache miss. */
|
|
82
|
+
getPhoto(id: string, signal?: AbortSignal): Promise<UnsplashPhoto>;
|
|
83
|
+
/** Fires the MANDATORY download trigger. Non-throwing — best-effort. */
|
|
84
|
+
trackDownload(downloadLocation: string, signal?: AbortSignal): Promise<void>;
|
|
85
|
+
}
|
|
86
|
+
export declare function createUnsplashClient(options: UnsplashClientOptions): UnsplashClient;
|
|
87
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@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AM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IACnE,wEAAwE;IACxE,aAAa,CAAC,gBAAgB,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7E;AAsDD,wBAAgB,oBAAoB,CACnC,OAAO,EAAE,qBAAqB,GAC5B,cAAc,CAiJhB"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { AssetSourceError } from "../../utils/errors.js";
|
|
2
|
+
function createTimedSignal(timeoutMs, caller) {
|
|
3
|
+
const controller = new AbortController();
|
|
4
|
+
let didTimeout = false;
|
|
5
|
+
const timer = setTimeout(()=>{
|
|
6
|
+
didTimeout = true;
|
|
7
|
+
controller.abort();
|
|
8
|
+
}, timeoutMs);
|
|
9
|
+
timer.unref?.();
|
|
10
|
+
const onCallerAbort = ()=>controller.abort();
|
|
11
|
+
if (caller) if (caller.aborted) controller.abort();
|
|
12
|
+
else caller.addEventListener("abort", onCallerAbort, {
|
|
13
|
+
once: true
|
|
14
|
+
});
|
|
15
|
+
return {
|
|
16
|
+
signal: controller.signal,
|
|
17
|
+
timedOut: ()=>didTimeout,
|
|
18
|
+
dispose: ()=>{
|
|
19
|
+
clearTimeout(timer);
|
|
20
|
+
caller?.removeEventListener("abort", onCallerAbort);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function isAbortError(error) {
|
|
25
|
+
return "object" == typeof error && null !== error && "AbortError" === error.name;
|
|
26
|
+
}
|
|
27
|
+
function parseRetryAfterMs(headers) {
|
|
28
|
+
const retryAfter = headers.get("retry-after");
|
|
29
|
+
if (null !== retryAfter) {
|
|
30
|
+
const seconds = Number(retryAfter);
|
|
31
|
+
if (Number.isFinite(seconds)) return 1000 * seconds;
|
|
32
|
+
}
|
|
33
|
+
const reset = headers.get("x-ratelimit-reset");
|
|
34
|
+
if (null !== reset) {
|
|
35
|
+
const epochSeconds = Number(reset);
|
|
36
|
+
if (Number.isFinite(epochSeconds)) {
|
|
37
|
+
const ms = 1000 * epochSeconds - Date.now();
|
|
38
|
+
if (ms > 0) return ms;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function mapHttpError(response) {
|
|
43
|
+
if (429 === response.status) {
|
|
44
|
+
const retryAfterMs = parseRetryAfterMs(response.headers);
|
|
45
|
+
return new AssetSourceError("PROVIDER_RATE_LIMITED", "Unsplash rate limit reached.", {
|
|
46
|
+
retryable: true,
|
|
47
|
+
status: 429,
|
|
48
|
+
...void 0 !== retryAfterMs ? {
|
|
49
|
+
retryAfterMs
|
|
50
|
+
} : {}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
if (401 === response.status || 403 === response.status) return new AssetSourceError("PROVIDER_UNAUTHORIZED", "Unsplash rejected the request — check the access key / proxy.", {
|
|
54
|
+
status: response.status
|
|
55
|
+
});
|
|
56
|
+
return new AssetSourceError("PROVIDER_BAD_RESPONSE", `Unsplash returned HTTP ${response.status}.`, {
|
|
57
|
+
status: response.status,
|
|
58
|
+
retryable: response.status >= 500
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function createUnsplashClient(options) {
|
|
62
|
+
const doFetch = options.fetch ?? globalThis.fetch;
|
|
63
|
+
const usingProxy = void 0 !== options.proxyEndpoint;
|
|
64
|
+
const base = (usingProxy ? String(options.proxyEndpoint) : "https://api.unsplash.com").replace(/\/$/, "");
|
|
65
|
+
const timeoutMs = options.timeoutMs ?? 15000;
|
|
66
|
+
const trackDownloadTimeoutMs = options.trackDownloadTimeoutMs ?? 6000;
|
|
67
|
+
const authHeaders = ()=>!usingProxy && options.accessKey ? {
|
|
68
|
+
Authorization: `Client-ID ${options.accessKey}`
|
|
69
|
+
} : {};
|
|
70
|
+
const readJson = async (response)=>{
|
|
71
|
+
try {
|
|
72
|
+
return await response.json();
|
|
73
|
+
} catch (cause) {
|
|
74
|
+
throw new AssetSourceError("PROVIDER_BAD_RESPONSE", "Unsplash returned an unreadable response.", {
|
|
75
|
+
cause
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
const requestJson = async (path, params, signal)=>{
|
|
80
|
+
const search = new URLSearchParams();
|
|
81
|
+
for (const [key, value] of Object.entries(params))if (void 0 !== value) search.set(key, String(value));
|
|
82
|
+
const query = search.toString();
|
|
83
|
+
const url = `${base}${path}${query ? `?${query}` : ""}`;
|
|
84
|
+
const timed = createTimedSignal(timeoutMs, signal);
|
|
85
|
+
try {
|
|
86
|
+
let response;
|
|
87
|
+
try {
|
|
88
|
+
response = await doFetch(url, {
|
|
89
|
+
headers: authHeaders(),
|
|
90
|
+
signal: timed.signal
|
|
91
|
+
});
|
|
92
|
+
} catch (cause) {
|
|
93
|
+
if (timed.timedOut()) throw new AssetSourceError("PROVIDER_NETWORK", `Unsplash request timed out after ${timeoutMs} ms.`, {
|
|
94
|
+
retryable: true,
|
|
95
|
+
cause
|
|
96
|
+
});
|
|
97
|
+
if (isAbortError(cause)) throw cause;
|
|
98
|
+
throw new AssetSourceError("PROVIDER_NETWORK", "Network error contacting Unsplash.", {
|
|
99
|
+
retryable: true,
|
|
100
|
+
cause
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
if (!response.ok) throw mapHttpError(response);
|
|
104
|
+
return await readJson(response);
|
|
105
|
+
} finally{
|
|
106
|
+
timed.dispose();
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
return {
|
|
110
|
+
async searchPhotos (params, signal) {
|
|
111
|
+
const body = await requestJson("/search/photos", {
|
|
112
|
+
query: params.query,
|
|
113
|
+
page: params.page,
|
|
114
|
+
per_page: params.perPage,
|
|
115
|
+
orientation: params.orientation,
|
|
116
|
+
content_filter: params.contentFilter
|
|
117
|
+
}, signal);
|
|
118
|
+
return {
|
|
119
|
+
total: body.total ?? 0,
|
|
120
|
+
results: body.results ?? []
|
|
121
|
+
};
|
|
122
|
+
},
|
|
123
|
+
async topicPhotos (slug, params, signal) {
|
|
124
|
+
return await requestJson(`/topics/${encodeURIComponent(slug)}/photos`, {
|
|
125
|
+
page: params.page,
|
|
126
|
+
per_page: params.perPage,
|
|
127
|
+
orientation: params.orientation
|
|
128
|
+
}, signal) ?? [];
|
|
129
|
+
},
|
|
130
|
+
async listTopics (signal) {
|
|
131
|
+
return await requestJson("/topics", {
|
|
132
|
+
per_page: 30
|
|
133
|
+
}, signal) ?? [];
|
|
134
|
+
},
|
|
135
|
+
async getPhoto (id, signal) {
|
|
136
|
+
return requestJson(`/photos/${encodeURIComponent(id)}`, {}, signal);
|
|
137
|
+
},
|
|
138
|
+
async trackDownload (downloadLocation, signal) {
|
|
139
|
+
const timed = createTimedSignal(trackDownloadTimeoutMs, signal);
|
|
140
|
+
try {
|
|
141
|
+
await doFetch(downloadLocation, {
|
|
142
|
+
headers: authHeaders(),
|
|
143
|
+
signal: timed.signal
|
|
144
|
+
});
|
|
145
|
+
} catch {} finally{
|
|
146
|
+
timed.dispose();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
export { createUnsplashClient };
|