@opensite/hooks 0.1.1 → 2.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 +191 -45
- package/dist/browser/opensite-hooks.umd.cjs +1 -1
- package/dist/browser/opensite-hooks.umd.js +1 -1
- package/dist/browser/opensite-hooks.umd.js.map +1 -1
- package/dist/core/index.cjs +7 -0
- package/dist/core/index.d.ts +13 -0
- package/dist/core/index.js +7 -0
- package/dist/core/useOpenGraphExtractor.cjs +59 -0
- package/dist/core/useOpenGraphExtractor.d.ts +64 -0
- package/dist/core/useOpenGraphExtractor.js +59 -0
- package/dist/core/useWebsiteExtractorBase.cjs +153 -0
- package/dist/core/useWebsiteExtractorBase.d.ts +9 -0
- package/dist/core/useWebsiteExtractorBase.js +153 -0
- package/dist/core/useWebsiteLinksExtractor.cjs +16 -0
- package/dist/core/useWebsiteLinksExtractor.d.ts +14 -0
- package/dist/core/useWebsiteLinksExtractor.js +16 -0
- package/dist/core/useWebsiteMetaExtractor.cjs +20 -0
- package/dist/core/useWebsiteMetaExtractor.d.ts +12 -0
- package/dist/core/useWebsiteMetaExtractor.js +20 -0
- package/dist/core/useWebsiteRssExtractor.cjs +15 -0
- package/dist/core/useWebsiteRssExtractor.d.ts +12 -0
- package/dist/core/useWebsiteRssExtractor.js +15 -0
- package/dist/core/useWebsiteSchemaExtractor.cjs +15 -0
- package/dist/core/useWebsiteSchemaExtractor.d.ts +11 -0
- package/dist/core/useWebsiteSchemaExtractor.js +15 -0
- package/dist/core/websiteExtractorService.cjs +66 -0
- package/dist/core/websiteExtractorService.d.ts +3 -0
- package/dist/core/websiteExtractorService.js +66 -0
- package/dist/core/websiteExtractorTypes.cjs +25 -0
- package/dist/core/websiteExtractorTypes.d.ts +65 -0
- package/dist/core/websiteExtractorTypes.js +25 -0
- package/package.json +90 -139
- package/dist/hooks/index.cjs +0 -16
- package/dist/hooks/index.d.ts +0 -24
- package/dist/hooks/index.js +0 -16
- package/dist/hooks/useBoolean.cjs +0 -1
- package/dist/hooks/useBoolean.d.ts +0 -2
- package/dist/hooks/useBoolean.js +0 -1
- package/dist/hooks/useCopyToClipboard.cjs +0 -1
- package/dist/hooks/useCopyToClipboard.d.ts +0 -2
- package/dist/hooks/useCopyToClipboard.js +0 -1
- package/dist/hooks/useDebounceCallback.cjs +0 -1
- package/dist/hooks/useDebounceCallback.d.ts +0 -2
- package/dist/hooks/useDebounceCallback.js +0 -1
- package/dist/hooks/useDebounceValue.cjs +0 -1
- package/dist/hooks/useDebounceValue.d.ts +0 -2
- package/dist/hooks/useDebounceValue.js +0 -1
- package/dist/hooks/useEventListener.cjs +0 -1
- package/dist/hooks/useEventListener.d.ts +0 -1
- package/dist/hooks/useEventListener.js +0 -1
- package/dist/hooks/useHover.cjs +0 -1
- package/dist/hooks/useHover.d.ts +0 -1
- package/dist/hooks/useHover.js +0 -1
- package/dist/hooks/useIsClient.cjs +0 -1
- package/dist/hooks/useIsClient.d.ts +0 -1
- package/dist/hooks/useIsClient.js +0 -1
- package/dist/hooks/useIsomorphicLayoutEffect.cjs +0 -1
- package/dist/hooks/useIsomorphicLayoutEffect.d.ts +0 -1
- package/dist/hooks/useIsomorphicLayoutEffect.js +0 -1
- package/dist/hooks/useLocalStorage.cjs +0 -1
- package/dist/hooks/useLocalStorage.d.ts +0 -2
- package/dist/hooks/useLocalStorage.js +0 -1
- package/dist/hooks/useMap.cjs +0 -1
- package/dist/hooks/useMap.d.ts +0 -2
- package/dist/hooks/useMap.js +0 -1
- package/dist/hooks/useMediaQuery.cjs +0 -1
- package/dist/hooks/useMediaQuery.d.ts +0 -2
- package/dist/hooks/useMediaQuery.js +0 -1
- package/dist/hooks/useOnClickOutside.cjs +0 -1
- package/dist/hooks/useOnClickOutside.d.ts +0 -1
- package/dist/hooks/useOnClickOutside.js +0 -1
- package/dist/hooks/usePrevious.cjs +0 -1
- package/dist/hooks/usePrevious.d.ts +0 -1
- package/dist/hooks/usePrevious.js +0 -1
- package/dist/hooks/useResizeObserver.cjs +0 -1
- package/dist/hooks/useResizeObserver.d.ts +0 -1
- package/dist/hooks/useResizeObserver.js +0 -1
- package/dist/hooks/useSessionStorage.cjs +0 -1
- package/dist/hooks/useSessionStorage.d.ts +0 -2
- package/dist/hooks/useSessionStorage.js +0 -1
- package/dist/hooks/useThrottle.cjs +0 -1
- package/dist/hooks/useThrottle.d.ts +0 -2
- package/dist/hooks/useThrottle.js +0 -1
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type WebsiteExtractorOptions, type WebsiteExtractorResult, type WebsiteExtractMeta } from "./websiteExtractorTypes.js";
|
|
2
|
+
interface UseWebsiteExtractorBaseConfig<TResponse extends WebsiteExtractMeta, TData> {
|
|
3
|
+
endpoint: string;
|
|
4
|
+
options: WebsiteExtractorOptions;
|
|
5
|
+
selectData: (payload: Omit<TResponse, keyof WebsiteExtractMeta>, raw: TResponse, meta: WebsiteExtractMeta) => TData;
|
|
6
|
+
shouldSkip?: (url: string) => boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare function useWebsiteExtractorBase<TResponse extends WebsiteExtractMeta, TData>(config: UseWebsiteExtractorBaseConfig<TResponse, TData>): WebsiteExtractorResult<TData, TResponse>;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { useDebounceCallback } from "./useDebounceCallback.js";
|
|
3
|
+
import { useDebounceValue } from "./useDebounceValue.js";
|
|
4
|
+
import { useIsClient } from "./useIsClient.js";
|
|
5
|
+
import { useMap } from "./useMap.js";
|
|
6
|
+
import { fetchWebsiteExtractor } from "./websiteExtractorService.js";
|
|
7
|
+
import { DEFAULT_EXTRACTOR_CACHE, DEFAULT_EXTRACTOR_DEBOUNCE_MS, DEFAULT_EXTRACTOR_ENABLED, DEFAULT_EXTRACTOR_REFRESH_DEBOUNCE_MS, DEFAULT_WEBSITE_EXTRACTOR_BASE_URL, extractWebsiteMeta, stripWebsiteMeta, } from "./websiteExtractorTypes.js";
|
|
8
|
+
export function useWebsiteExtractorBase(config) {
|
|
9
|
+
const { endpoint, options, selectData, shouldSkip } = config;
|
|
10
|
+
const isClient = useIsClient();
|
|
11
|
+
const [state, setState] = useState({
|
|
12
|
+
loading: false,
|
|
13
|
+
});
|
|
14
|
+
const [, cacheActions] = useMap();
|
|
15
|
+
const cacheEnabled = options.cache ?? DEFAULT_EXTRACTOR_CACHE;
|
|
16
|
+
const enabled = options.enabled ?? DEFAULT_EXTRACTOR_ENABLED;
|
|
17
|
+
const debounceMs = options.debounceMs ?? DEFAULT_EXTRACTOR_DEBOUNCE_MS;
|
|
18
|
+
const refreshDebounceMs = options.refreshDebounceMs ?? DEFAULT_EXTRACTOR_REFRESH_DEBOUNCE_MS;
|
|
19
|
+
const normalizedUrl = useMemo(() => {
|
|
20
|
+
return options.url?.trim() ?? "";
|
|
21
|
+
}, [options.url]);
|
|
22
|
+
const debouncedUrl = useDebounceValue(normalizedUrl, debounceMs);
|
|
23
|
+
const refreshCounterRef = useRef(0);
|
|
24
|
+
const lastRefreshHandledRef = useRef(0);
|
|
25
|
+
const [refreshToken, setRefreshToken] = useState(0);
|
|
26
|
+
const { debouncedCallback: scheduleRefresh, cancel: cancelRefresh } = useDebounceCallback(() => {
|
|
27
|
+
refreshCounterRef.current += 1;
|
|
28
|
+
setRefreshToken(refreshCounterRef.current);
|
|
29
|
+
}, refreshDebounceMs);
|
|
30
|
+
const refresh = useCallback(() => {
|
|
31
|
+
scheduleRefresh();
|
|
32
|
+
}, [scheduleRefresh]);
|
|
33
|
+
useEffect(() => () => cancelRefresh(), [cancelRefresh]);
|
|
34
|
+
const requestKey = useMemo(() => {
|
|
35
|
+
if (!debouncedUrl) {
|
|
36
|
+
return "";
|
|
37
|
+
}
|
|
38
|
+
const baseUrl = options.baseUrl ?? DEFAULT_WEBSITE_EXTRACTOR_BASE_URL;
|
|
39
|
+
const apiKey = options.apiKey ?? "";
|
|
40
|
+
return `${endpoint}:${baseUrl}:${apiKey}:${debouncedUrl}`;
|
|
41
|
+
}, [
|
|
42
|
+
endpoint,
|
|
43
|
+
options.apiKey,
|
|
44
|
+
options.baseUrl,
|
|
45
|
+
debouncedUrl,
|
|
46
|
+
]);
|
|
47
|
+
const inFlightControllerRef = useRef(null);
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (!isClient) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (!enabled || !debouncedUrl) {
|
|
53
|
+
setState({ loading: false });
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (shouldSkip?.(debouncedUrl)) {
|
|
57
|
+
setState({ loading: false });
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const forceRefresh = refreshToken !== lastRefreshHandledRef.current;
|
|
61
|
+
if (forceRefresh) {
|
|
62
|
+
lastRefreshHandledRef.current = refreshToken;
|
|
63
|
+
}
|
|
64
|
+
if (cacheEnabled && !forceRefresh) {
|
|
65
|
+
const cached = cacheActions.get(requestKey);
|
|
66
|
+
if (cached) {
|
|
67
|
+
setState({
|
|
68
|
+
loading: false,
|
|
69
|
+
data: cached.data,
|
|
70
|
+
raw: cached.raw,
|
|
71
|
+
meta: cached.meta,
|
|
72
|
+
});
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
inFlightControllerRef.current?.abort();
|
|
77
|
+
const controller = new AbortController();
|
|
78
|
+
inFlightControllerRef.current = controller;
|
|
79
|
+
setState((prev) => ({
|
|
80
|
+
...prev,
|
|
81
|
+
loading: true,
|
|
82
|
+
error: undefined,
|
|
83
|
+
}));
|
|
84
|
+
fetchWebsiteExtractor({
|
|
85
|
+
endpoint,
|
|
86
|
+
url: debouncedUrl,
|
|
87
|
+
apiKey: options.apiKey,
|
|
88
|
+
baseUrl: options.baseUrl,
|
|
89
|
+
signal: controller.signal,
|
|
90
|
+
})
|
|
91
|
+
.then((result) => {
|
|
92
|
+
if (controller.signal.aborted) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (!result.ok) {
|
|
96
|
+
setState((prev) => ({
|
|
97
|
+
...prev,
|
|
98
|
+
loading: false,
|
|
99
|
+
error: result.error,
|
|
100
|
+
}));
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const raw = result.response;
|
|
104
|
+
const meta = extractWebsiteMeta(raw);
|
|
105
|
+
const payload = stripWebsiteMeta(raw);
|
|
106
|
+
const data = selectData(payload, raw, meta);
|
|
107
|
+
if (cacheEnabled) {
|
|
108
|
+
cacheActions.set(requestKey, { data, raw, meta });
|
|
109
|
+
}
|
|
110
|
+
setState({
|
|
111
|
+
loading: false,
|
|
112
|
+
data,
|
|
113
|
+
raw,
|
|
114
|
+
meta,
|
|
115
|
+
});
|
|
116
|
+
})
|
|
117
|
+
.catch((error) => {
|
|
118
|
+
if (controller.signal.aborted) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
setState((prev) => ({
|
|
122
|
+
...prev,
|
|
123
|
+
loading: false,
|
|
124
|
+
error: {
|
|
125
|
+
message: error instanceof Error
|
|
126
|
+
? error.message
|
|
127
|
+
: "Request failed unexpectedly.",
|
|
128
|
+
raw: error,
|
|
129
|
+
},
|
|
130
|
+
}));
|
|
131
|
+
});
|
|
132
|
+
return () => {
|
|
133
|
+
controller.abort();
|
|
134
|
+
};
|
|
135
|
+
}, [
|
|
136
|
+
cacheActions,
|
|
137
|
+
cacheEnabled,
|
|
138
|
+
debouncedUrl,
|
|
139
|
+
enabled,
|
|
140
|
+
endpoint,
|
|
141
|
+
isClient,
|
|
142
|
+
options.apiKey,
|
|
143
|
+
options.baseUrl,
|
|
144
|
+
requestKey,
|
|
145
|
+
refreshToken,
|
|
146
|
+
selectData,
|
|
147
|
+
shouldSkip,
|
|
148
|
+
]);
|
|
149
|
+
return useMemo(() => ({
|
|
150
|
+
...state,
|
|
151
|
+
refresh,
|
|
152
|
+
}), [refresh, state]);
|
|
153
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
import { useWebsiteExtractorBase } from "./useWebsiteExtractorBase.js";
|
|
3
|
+
export function useWebsiteLinksExtractor(options) {
|
|
4
|
+
const selectData = useCallback((payload) => {
|
|
5
|
+
return {
|
|
6
|
+
totalLinks: payload.totalLinks ?? 0,
|
|
7
|
+
uniqueDomains: payload.uniqueDomains ?? 0,
|
|
8
|
+
links: payload.links ?? [],
|
|
9
|
+
};
|
|
10
|
+
}, []);
|
|
11
|
+
return useWebsiteExtractorBase({
|
|
12
|
+
endpoint: "links",
|
|
13
|
+
options,
|
|
14
|
+
selectData,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { WebsiteExtractorOptions, WebsiteExtractorResponse, WebsiteExtractorResult } from "./websiteExtractorTypes.js";
|
|
2
|
+
export interface WebsiteLinkRecord {
|
|
3
|
+
url: string;
|
|
4
|
+
text: string;
|
|
5
|
+
isExternal: boolean;
|
|
6
|
+
domain?: string | null;
|
|
7
|
+
}
|
|
8
|
+
export interface WebsiteLinksPayload {
|
|
9
|
+
totalLinks: number;
|
|
10
|
+
uniqueDomains: number;
|
|
11
|
+
links: WebsiteLinkRecord[];
|
|
12
|
+
}
|
|
13
|
+
export type WebsiteLinksResponse = WebsiteExtractorResponse<WebsiteLinksPayload>;
|
|
14
|
+
export declare function useWebsiteLinksExtractor(options: WebsiteExtractorOptions): WebsiteExtractorResult<WebsiteLinksPayload, WebsiteLinksResponse>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
import { useWebsiteExtractorBase } from "./useWebsiteExtractorBase.js";
|
|
3
|
+
export function useWebsiteLinksExtractor(options) {
|
|
4
|
+
const selectData = useCallback((payload) => {
|
|
5
|
+
return {
|
|
6
|
+
totalLinks: payload.totalLinks ?? 0,
|
|
7
|
+
uniqueDomains: payload.uniqueDomains ?? 0,
|
|
8
|
+
links: payload.links ?? [],
|
|
9
|
+
};
|
|
10
|
+
}, []);
|
|
11
|
+
return useWebsiteExtractorBase({
|
|
12
|
+
endpoint: "links",
|
|
13
|
+
options,
|
|
14
|
+
selectData,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
import { useWebsiteExtractorBase } from "./useWebsiteExtractorBase.js";
|
|
3
|
+
export function useWebsiteMetaExtractor(options) {
|
|
4
|
+
const selectData = useCallback((payload) => {
|
|
5
|
+
return {
|
|
6
|
+
title: payload.title ?? undefined,
|
|
7
|
+
description: payload.description ?? undefined,
|
|
8
|
+
language: payload.language ?? undefined,
|
|
9
|
+
canonicalUrl: payload.canonicalUrl ?? undefined,
|
|
10
|
+
feedUrl: payload.feedUrl ?? null,
|
|
11
|
+
textContentLength: payload.textContentLength ?? undefined,
|
|
12
|
+
metaTags: payload.metaTags ?? {},
|
|
13
|
+
};
|
|
14
|
+
}, []);
|
|
15
|
+
return useWebsiteExtractorBase({
|
|
16
|
+
endpoint: "meta",
|
|
17
|
+
options,
|
|
18
|
+
selectData,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { WebsiteExtractorOptions, WebsiteExtractorResponse, WebsiteExtractorResult } from "./websiteExtractorTypes.js";
|
|
2
|
+
export interface WebsiteMetaPayload {
|
|
3
|
+
title?: string | null;
|
|
4
|
+
description?: string | null;
|
|
5
|
+
language?: string | null;
|
|
6
|
+
canonicalUrl?: string | null;
|
|
7
|
+
feedUrl?: string | null;
|
|
8
|
+
textContentLength?: number | null;
|
|
9
|
+
metaTags: Record<string, string>;
|
|
10
|
+
}
|
|
11
|
+
export type WebsiteMetaResponse = WebsiteExtractorResponse<WebsiteMetaPayload>;
|
|
12
|
+
export declare function useWebsiteMetaExtractor(options: WebsiteExtractorOptions): WebsiteExtractorResult<WebsiteMetaPayload, WebsiteMetaResponse>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
import { useWebsiteExtractorBase } from "./useWebsiteExtractorBase.js";
|
|
3
|
+
export function useWebsiteMetaExtractor(options) {
|
|
4
|
+
const selectData = useCallback((payload) => {
|
|
5
|
+
return {
|
|
6
|
+
title: payload.title ?? undefined,
|
|
7
|
+
description: payload.description ?? undefined,
|
|
8
|
+
language: payload.language ?? undefined,
|
|
9
|
+
canonicalUrl: payload.canonicalUrl ?? undefined,
|
|
10
|
+
feedUrl: payload.feedUrl ?? null,
|
|
11
|
+
textContentLength: payload.textContentLength ?? undefined,
|
|
12
|
+
metaTags: payload.metaTags ?? {},
|
|
13
|
+
};
|
|
14
|
+
}, []);
|
|
15
|
+
return useWebsiteExtractorBase({
|
|
16
|
+
endpoint: "meta",
|
|
17
|
+
options,
|
|
18
|
+
selectData,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
import { useWebsiteExtractorBase } from "./useWebsiteExtractorBase.js";
|
|
3
|
+
export function useWebsiteRssExtractor(options) {
|
|
4
|
+
const selectData = useCallback((payload) => {
|
|
5
|
+
return {
|
|
6
|
+
feedUrl: payload.feedUrl ?? null,
|
|
7
|
+
feeds: payload.feeds ?? [],
|
|
8
|
+
};
|
|
9
|
+
}, []);
|
|
10
|
+
return useWebsiteExtractorBase({
|
|
11
|
+
endpoint: "rss",
|
|
12
|
+
options,
|
|
13
|
+
selectData,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { WebsiteExtractorOptions, WebsiteExtractorResponse, WebsiteExtractorResult } from "./websiteExtractorTypes.js";
|
|
2
|
+
export interface WebsiteRssFeed {
|
|
3
|
+
url: string;
|
|
4
|
+
feedType: string;
|
|
5
|
+
title?: string | null;
|
|
6
|
+
}
|
|
7
|
+
export interface WebsiteRssPayload {
|
|
8
|
+
feedUrl?: string | null;
|
|
9
|
+
feeds: WebsiteRssFeed[];
|
|
10
|
+
}
|
|
11
|
+
export type WebsiteRssResponse = WebsiteExtractorResponse<WebsiteRssPayload>;
|
|
12
|
+
export declare function useWebsiteRssExtractor(options: WebsiteExtractorOptions): WebsiteExtractorResult<WebsiteRssPayload, WebsiteRssResponse>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
import { useWebsiteExtractorBase } from "./useWebsiteExtractorBase.js";
|
|
3
|
+
export function useWebsiteRssExtractor(options) {
|
|
4
|
+
const selectData = useCallback((payload) => {
|
|
5
|
+
return {
|
|
6
|
+
feedUrl: payload.feedUrl ?? null,
|
|
7
|
+
feeds: payload.feeds ?? [],
|
|
8
|
+
};
|
|
9
|
+
}, []);
|
|
10
|
+
return useWebsiteExtractorBase({
|
|
11
|
+
endpoint: "rss",
|
|
12
|
+
options,
|
|
13
|
+
selectData,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
import { useWebsiteExtractorBase } from "./useWebsiteExtractorBase.js";
|
|
3
|
+
export function useWebsiteSchemaExtractor(options) {
|
|
4
|
+
const selectData = useCallback((payload) => {
|
|
5
|
+
return {
|
|
6
|
+
schema: payload.schema ?? [],
|
|
7
|
+
schemaTypes: payload.schemaTypes ?? [],
|
|
8
|
+
};
|
|
9
|
+
}, []);
|
|
10
|
+
return useWebsiteExtractorBase({
|
|
11
|
+
endpoint: "schema",
|
|
12
|
+
options,
|
|
13
|
+
selectData,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { WebsiteExtractorOptions, WebsiteExtractorResponse, WebsiteExtractorResult } from "./websiteExtractorTypes.js";
|
|
2
|
+
export interface WebsiteSchemaRecord {
|
|
3
|
+
schema_type: string;
|
|
4
|
+
value: Record<string, unknown>;
|
|
5
|
+
}
|
|
6
|
+
export interface WebsiteSchemaPayload {
|
|
7
|
+
schema: WebsiteSchemaRecord[];
|
|
8
|
+
schemaTypes: string[];
|
|
9
|
+
}
|
|
10
|
+
export type WebsiteSchemaResponse = WebsiteExtractorResponse<WebsiteSchemaPayload>;
|
|
11
|
+
export declare function useWebsiteSchemaExtractor(options: WebsiteExtractorOptions): WebsiteExtractorResult<WebsiteSchemaPayload, WebsiteSchemaResponse>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
import { useWebsiteExtractorBase } from "./useWebsiteExtractorBase.js";
|
|
3
|
+
export function useWebsiteSchemaExtractor(options) {
|
|
4
|
+
const selectData = useCallback((payload) => {
|
|
5
|
+
return {
|
|
6
|
+
schema: payload.schema ?? [],
|
|
7
|
+
schemaTypes: payload.schemaTypes ?? [],
|
|
8
|
+
};
|
|
9
|
+
}, []);
|
|
10
|
+
return useWebsiteExtractorBase({
|
|
11
|
+
endpoint: "schema",
|
|
12
|
+
options,
|
|
13
|
+
selectData,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { DEFAULT_WEBSITE_EXTRACTOR_BASE_URL, } from "./websiteExtractorTypes.js";
|
|
2
|
+
const EXTRACTOR_PATH = "/api/v1/extract";
|
|
3
|
+
function normalizeBaseUrl(baseUrl) {
|
|
4
|
+
return baseUrl.replace(/\/+$/, "");
|
|
5
|
+
}
|
|
6
|
+
export function buildWebsiteExtractorUrl(request) {
|
|
7
|
+
const baseUrl = normalizeBaseUrl(request.baseUrl ?? DEFAULT_WEBSITE_EXTRACTOR_BASE_URL);
|
|
8
|
+
const params = new URLSearchParams();
|
|
9
|
+
if (request.apiKey) {
|
|
10
|
+
params.set("api_key", request.apiKey);
|
|
11
|
+
}
|
|
12
|
+
params.set("url", request.url);
|
|
13
|
+
return `${baseUrl}${EXTRACTOR_PATH}/${request.endpoint}?${params.toString()}`;
|
|
14
|
+
}
|
|
15
|
+
export async function fetchWebsiteExtractor(request) {
|
|
16
|
+
if (!request.url || request.url.trim().length === 0) {
|
|
17
|
+
return {
|
|
18
|
+
ok: false,
|
|
19
|
+
error: {
|
|
20
|
+
message: "URL is required.",
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const endpoint = buildWebsiteExtractorUrl(request);
|
|
25
|
+
try {
|
|
26
|
+
const response = await fetch(endpoint, {
|
|
27
|
+
method: "GET",
|
|
28
|
+
signal: request.signal,
|
|
29
|
+
});
|
|
30
|
+
let payload = null;
|
|
31
|
+
try {
|
|
32
|
+
payload = await response.json();
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
payload = null;
|
|
36
|
+
}
|
|
37
|
+
if (!response.ok) {
|
|
38
|
+
const errorPayload = payload;
|
|
39
|
+
const error = {
|
|
40
|
+
message: errorPayload?.error ??
|
|
41
|
+
`Request failed with status ${response.status}.`,
|
|
42
|
+
status: errorPayload?.status ?? response.status,
|
|
43
|
+
raw: payload,
|
|
44
|
+
};
|
|
45
|
+
return { ok: false, error };
|
|
46
|
+
}
|
|
47
|
+
return { ok: true, response: payload };
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
if (request.signal?.aborted) {
|
|
51
|
+
return {
|
|
52
|
+
ok: false,
|
|
53
|
+
error: {
|
|
54
|
+
message: "Request aborted.",
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
ok: false,
|
|
60
|
+
error: {
|
|
61
|
+
message: error instanceof Error ? error.message : "Request failed unexpectedly.",
|
|
62
|
+
raw: error,
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { type WebsiteExtractorClientResult, type WebsiteExtractorRequest } from "./websiteExtractorTypes.js";
|
|
2
|
+
export declare function buildWebsiteExtractorUrl(request: WebsiteExtractorRequest): string;
|
|
3
|
+
export declare function fetchWebsiteExtractor<TResponse>(request: WebsiteExtractorRequest): Promise<WebsiteExtractorClientResult<TResponse>>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { DEFAULT_WEBSITE_EXTRACTOR_BASE_URL, } from "./websiteExtractorTypes.js";
|
|
2
|
+
const EXTRACTOR_PATH = "/api/v1/extract";
|
|
3
|
+
function normalizeBaseUrl(baseUrl) {
|
|
4
|
+
return baseUrl.replace(/\/+$/, "");
|
|
5
|
+
}
|
|
6
|
+
export function buildWebsiteExtractorUrl(request) {
|
|
7
|
+
const baseUrl = normalizeBaseUrl(request.baseUrl ?? DEFAULT_WEBSITE_EXTRACTOR_BASE_URL);
|
|
8
|
+
const params = new URLSearchParams();
|
|
9
|
+
if (request.apiKey) {
|
|
10
|
+
params.set("api_key", request.apiKey);
|
|
11
|
+
}
|
|
12
|
+
params.set("url", request.url);
|
|
13
|
+
return `${baseUrl}${EXTRACTOR_PATH}/${request.endpoint}?${params.toString()}`;
|
|
14
|
+
}
|
|
15
|
+
export async function fetchWebsiteExtractor(request) {
|
|
16
|
+
if (!request.url || request.url.trim().length === 0) {
|
|
17
|
+
return {
|
|
18
|
+
ok: false,
|
|
19
|
+
error: {
|
|
20
|
+
message: "URL is required.",
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const endpoint = buildWebsiteExtractorUrl(request);
|
|
25
|
+
try {
|
|
26
|
+
const response = await fetch(endpoint, {
|
|
27
|
+
method: "GET",
|
|
28
|
+
signal: request.signal,
|
|
29
|
+
});
|
|
30
|
+
let payload = null;
|
|
31
|
+
try {
|
|
32
|
+
payload = await response.json();
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
payload = null;
|
|
36
|
+
}
|
|
37
|
+
if (!response.ok) {
|
|
38
|
+
const errorPayload = payload;
|
|
39
|
+
const error = {
|
|
40
|
+
message: errorPayload?.error ??
|
|
41
|
+
`Request failed with status ${response.status}.`,
|
|
42
|
+
status: errorPayload?.status ?? response.status,
|
|
43
|
+
raw: payload,
|
|
44
|
+
};
|
|
45
|
+
return { ok: false, error };
|
|
46
|
+
}
|
|
47
|
+
return { ok: true, response: payload };
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
if (request.signal?.aborted) {
|
|
51
|
+
return {
|
|
52
|
+
ok: false,
|
|
53
|
+
error: {
|
|
54
|
+
message: "Request aborted.",
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
ok: false,
|
|
60
|
+
error: {
|
|
61
|
+
message: error instanceof Error ? error.message : "Request failed unexpectedly.",
|
|
62
|
+
raw: error,
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export const DEFAULT_WEBSITE_EXTRACTOR_BASE_URL = "https://octane.buzz";
|
|
2
|
+
export const DEFAULT_EXTRACTOR_DEBOUNCE_MS = 250;
|
|
3
|
+
export const DEFAULT_EXTRACTOR_REFRESH_DEBOUNCE_MS = 150;
|
|
4
|
+
export const DEFAULT_EXTRACTOR_ENABLED = true;
|
|
5
|
+
export const DEFAULT_EXTRACTOR_CACHE = true;
|
|
6
|
+
export function extractWebsiteMeta(response) {
|
|
7
|
+
const { requestedUrl, finalUrl, url, normalizedUrl, status, contentType, fetchedAt, bodyBytes, bodyTruncated, maxBodyBytes, cache, } = response;
|
|
8
|
+
return {
|
|
9
|
+
requestedUrl,
|
|
10
|
+
finalUrl,
|
|
11
|
+
url,
|
|
12
|
+
normalizedUrl,
|
|
13
|
+
status,
|
|
14
|
+
contentType,
|
|
15
|
+
fetchedAt,
|
|
16
|
+
bodyBytes,
|
|
17
|
+
bodyTruncated,
|
|
18
|
+
maxBodyBytes,
|
|
19
|
+
cache,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export function stripWebsiteMeta(response) {
|
|
23
|
+
const { requestedUrl: _requestedUrl, finalUrl: _finalUrl, url: _url, normalizedUrl: _normalizedUrl, status: _status, contentType: _contentType, fetchedAt: _fetchedAt, bodyBytes: _bodyBytes, bodyTruncated: _bodyTruncated, maxBodyBytes: _maxBodyBytes, cache: _cache, ...payload } = response;
|
|
24
|
+
return payload;
|
|
25
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export interface WebsiteExtractCacheMeta {
|
|
2
|
+
hit: boolean;
|
|
3
|
+
ageSeconds: number;
|
|
4
|
+
ttlSeconds: number;
|
|
5
|
+
staleWhileRevalidateSeconds: number;
|
|
6
|
+
}
|
|
7
|
+
export interface WebsiteExtractMeta {
|
|
8
|
+
requestedUrl: string;
|
|
9
|
+
finalUrl: string;
|
|
10
|
+
url: string;
|
|
11
|
+
normalizedUrl: string;
|
|
12
|
+
status: number;
|
|
13
|
+
contentType: string;
|
|
14
|
+
fetchedAt: string;
|
|
15
|
+
bodyBytes: number;
|
|
16
|
+
bodyTruncated: boolean;
|
|
17
|
+
maxBodyBytes: number;
|
|
18
|
+
cache: WebsiteExtractCacheMeta;
|
|
19
|
+
}
|
|
20
|
+
export interface WebsiteExtractorError {
|
|
21
|
+
message: string;
|
|
22
|
+
status?: number;
|
|
23
|
+
raw?: unknown;
|
|
24
|
+
}
|
|
25
|
+
export interface WebsiteExtractorOptions {
|
|
26
|
+
url?: string;
|
|
27
|
+
apiKey?: string;
|
|
28
|
+
baseUrl?: string;
|
|
29
|
+
debounceMs?: number;
|
|
30
|
+
refreshDebounceMs?: number;
|
|
31
|
+
enabled?: boolean;
|
|
32
|
+
cache?: boolean;
|
|
33
|
+
}
|
|
34
|
+
export interface WebsiteExtractorState<TData, TRaw = TData> {
|
|
35
|
+
loading: boolean;
|
|
36
|
+
data?: TData;
|
|
37
|
+
raw?: TRaw;
|
|
38
|
+
meta?: WebsiteExtractMeta;
|
|
39
|
+
error?: WebsiteExtractorError;
|
|
40
|
+
}
|
|
41
|
+
export interface WebsiteExtractorResult<TData, TRaw = TData> extends WebsiteExtractorState<TData, TRaw> {
|
|
42
|
+
refresh: () => void;
|
|
43
|
+
}
|
|
44
|
+
export type WebsiteExtractorResponse<TPayload> = WebsiteExtractMeta & TPayload;
|
|
45
|
+
export interface WebsiteExtractorRequest {
|
|
46
|
+
endpoint: string;
|
|
47
|
+
url: string;
|
|
48
|
+
apiKey?: string;
|
|
49
|
+
baseUrl?: string;
|
|
50
|
+
signal?: AbortSignal;
|
|
51
|
+
}
|
|
52
|
+
export type WebsiteExtractorClientResult<T> = {
|
|
53
|
+
ok: true;
|
|
54
|
+
response: T;
|
|
55
|
+
} | {
|
|
56
|
+
ok: false;
|
|
57
|
+
error: WebsiteExtractorError;
|
|
58
|
+
};
|
|
59
|
+
export declare const DEFAULT_WEBSITE_EXTRACTOR_BASE_URL = "https://octane.buzz";
|
|
60
|
+
export declare const DEFAULT_EXTRACTOR_DEBOUNCE_MS = 250;
|
|
61
|
+
export declare const DEFAULT_EXTRACTOR_REFRESH_DEBOUNCE_MS = 150;
|
|
62
|
+
export declare const DEFAULT_EXTRACTOR_ENABLED = true;
|
|
63
|
+
export declare const DEFAULT_EXTRACTOR_CACHE = true;
|
|
64
|
+
export declare function extractWebsiteMeta(response: WebsiteExtractMeta): WebsiteExtractMeta;
|
|
65
|
+
export declare function stripWebsiteMeta<TResponse extends WebsiteExtractMeta>(response: TResponse): Omit<TResponse, keyof WebsiteExtractMeta>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export const DEFAULT_WEBSITE_EXTRACTOR_BASE_URL = "https://octane.buzz";
|
|
2
|
+
export const DEFAULT_EXTRACTOR_DEBOUNCE_MS = 250;
|
|
3
|
+
export const DEFAULT_EXTRACTOR_REFRESH_DEBOUNCE_MS = 150;
|
|
4
|
+
export const DEFAULT_EXTRACTOR_ENABLED = true;
|
|
5
|
+
export const DEFAULT_EXTRACTOR_CACHE = true;
|
|
6
|
+
export function extractWebsiteMeta(response) {
|
|
7
|
+
const { requestedUrl, finalUrl, url, normalizedUrl, status, contentType, fetchedAt, bodyBytes, bodyTruncated, maxBodyBytes, cache, } = response;
|
|
8
|
+
return {
|
|
9
|
+
requestedUrl,
|
|
10
|
+
finalUrl,
|
|
11
|
+
url,
|
|
12
|
+
normalizedUrl,
|
|
13
|
+
status,
|
|
14
|
+
contentType,
|
|
15
|
+
fetchedAt,
|
|
16
|
+
bodyBytes,
|
|
17
|
+
bodyTruncated,
|
|
18
|
+
maxBodyBytes,
|
|
19
|
+
cache,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export function stripWebsiteMeta(response) {
|
|
23
|
+
const { requestedUrl: _requestedUrl, finalUrl: _finalUrl, url: _url, normalizedUrl: _normalizedUrl, status: _status, contentType: _contentType, fetchedAt: _fetchedAt, bodyBytes: _bodyBytes, bodyTruncated: _bodyTruncated, maxBodyBytes: _maxBodyBytes, cache: _cache, ...payload } = response;
|
|
24
|
+
return payload;
|
|
25
|
+
}
|