@sitecore-content-sdk/core 0.2.0 → 0.2.1-canary.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/dist/cjs/client/sitecore-client.js +1 -6
- package/dist/cjs/editing/rest-component-layout-service.js +26 -45
- package/dist/cjs/site/graphql-robots-service.js +1 -0
- package/dist/esm/client/sitecore-client.js +1 -6
- package/dist/esm/editing/rest-component-layout-service.js +23 -45
- package/dist/esm/site/graphql-robots-service.js +1 -0
- package/package.json +2 -2
- package/types/editing/rest-component-layout-service.d.ts +23 -58
- package/types/native-fetcher.d.ts +0 -7
- package/types/site/graphql-robots-service.d.ts +1 -0
- package/dist/cjs/data-fetcher.js +0 -22
- package/dist/esm/data-fetcher.js +0 -17
- package/types/data-fetcher.d.ts +0 -34
|
@@ -319,12 +319,7 @@ class SitecoreClient {
|
|
|
319
319
|
return new site_1.GraphQLErrorPagesService(Object.assign(Object.assign({}, this.initOptions), { language: this.initOptions.defaultLanguage, clientFactory: this.clientFactory }));
|
|
320
320
|
}
|
|
321
321
|
getComponentService() {
|
|
322
|
-
|
|
323
|
-
return new editing_1.RestComponentLayoutService({
|
|
324
|
-
apiHost: (_a = this.initOptions.api.local) === null || _a === void 0 ? void 0 : _a.apiHost,
|
|
325
|
-
apiKey: (_b = this.initOptions.api.local) === null || _b === void 0 ? void 0 : _b.apiKey,
|
|
326
|
-
siteName: this.initOptions.defaultSite,
|
|
327
|
-
});
|
|
322
|
+
return new editing_1.RestComponentLayoutService(this.initOptions.api.edge);
|
|
328
323
|
}
|
|
329
324
|
getSitePathService() {
|
|
330
325
|
return new site_1.GraphQLSitePathService({
|
|
@@ -1,47 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.RestComponentLayoutService = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
7
|
+
const native_fetcher_1 = require("../native-fetcher");
|
|
8
|
+
const debug_1 = __importDefault(require("../debug"));
|
|
9
|
+
const constants_1 = require("../constants");
|
|
10
|
+
const utils_1 = require("../utils");
|
|
6
11
|
/**
|
|
7
|
-
* REST service that enables
|
|
8
|
-
* Makes a request to /sitecore/api/layout/component in 'library' mode in Pages.
|
|
12
|
+
* REST service that enables design Library functionality
|
|
9
13
|
* Returns layoutData for one single rendered component
|
|
10
14
|
*/
|
|
11
15
|
class RestComponentLayoutService {
|
|
12
16
|
constructor(config) {
|
|
13
17
|
this.config = config;
|
|
14
|
-
this.getFetcher = (req, res) => {
|
|
15
|
-
return this.config.dataFetcherResolver
|
|
16
|
-
? this.config.dataFetcherResolver(req, res)
|
|
17
|
-
: this.getDefaultFetcher(req);
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* Provides default @see NativeDataFetcher data fetcher
|
|
21
|
-
* @param {IncomingMessage} [req] Request instance
|
|
22
|
-
* @returns default fetcher
|
|
23
|
-
*/
|
|
24
|
-
this.getDefaultFetcher = (req) => {
|
|
25
|
-
var _a;
|
|
26
|
-
const config = {
|
|
27
|
-
debugger: __1.debug.editing,
|
|
28
|
-
};
|
|
29
|
-
const nativeFetcher = new __1.NativeDataFetcher(config);
|
|
30
|
-
const headers = req && Object.assign(Object.assign({}, req.headers), (((_a = req.socket) === null || _a === void 0 ? void 0 : _a.remoteAddress) ? { 'X-Forwarded-For': req.socket.remoteAddress } : {}));
|
|
31
|
-
const fetcher = (url, data) => {
|
|
32
|
-
data = Object.assign(Object.assign({}, data), { headers: headers });
|
|
33
|
-
return nativeFetcher.fetch(url, data);
|
|
34
|
-
};
|
|
35
|
-
return fetcher;
|
|
36
|
-
};
|
|
37
18
|
}
|
|
38
|
-
fetchComponentData(params
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
19
|
+
fetchComponentData(params) {
|
|
20
|
+
const config = { debugger: debug_1.default.layout };
|
|
21
|
+
const fetcher = new native_fetcher_1.NativeDataFetcher(config);
|
|
22
|
+
debug_1.default.layout('fetching component with uid %s for %s %s %s %s', params.componentUid, params.itemId, params.language, params.siteName, params.dataSourceId);
|
|
23
|
+
const fetchUrl = this.getFetchUrl(params);
|
|
24
|
+
return fetcher
|
|
25
|
+
.get(fetchUrl)
|
|
26
|
+
.then((response) => response.data)
|
|
27
|
+
.catch((error) => {
|
|
45
28
|
var _a;
|
|
46
29
|
if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 404) {
|
|
47
30
|
return error.response.data;
|
|
@@ -49,19 +32,10 @@ class RestComponentLayoutService {
|
|
|
49
32
|
throw error;
|
|
50
33
|
});
|
|
51
34
|
}
|
|
52
|
-
/**
|
|
53
|
-
* Resolves layout service url
|
|
54
|
-
* @param {string} apiType which layout service API to call ('render' or 'placeholder')
|
|
55
|
-
* @returns the layout service url
|
|
56
|
-
*/
|
|
57
|
-
resolveLayoutServiceUrl(apiType) {
|
|
58
|
-
const { apiHost = '', configurationName = 'jss' } = this.config;
|
|
59
|
-
return `${apiHost}/sitecore/api/layout/${apiType}/${configurationName}`;
|
|
60
|
-
}
|
|
61
35
|
getComponentFetchParams(params) {
|
|
62
36
|
// exclude undefined params with this one simple trick
|
|
63
37
|
return JSON.parse(JSON.stringify({
|
|
64
|
-
|
|
38
|
+
sitecoreContextId: this.config.contextId,
|
|
65
39
|
item: params.itemId,
|
|
66
40
|
uid: params.componentUid,
|
|
67
41
|
dataSourceId: params.dataSourceId,
|
|
@@ -69,8 +43,15 @@ class RestComponentLayoutService {
|
|
|
69
43
|
version: params.version,
|
|
70
44
|
sc_site: params.siteName,
|
|
71
45
|
sc_lang: params.language || 'en',
|
|
72
|
-
sc_mode: params.editMode,
|
|
73
46
|
}));
|
|
74
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Get the fetch URL for the partial layout data endpoint
|
|
50
|
+
* @param {ComponentLayoutRequestParams} params - The parameters for the request
|
|
51
|
+
* @returns {string} The fetch URL for the component data
|
|
52
|
+
*/
|
|
53
|
+
getFetchUrl(params) {
|
|
54
|
+
return (0, utils_1.resolveUrl)(`${this.config.edgeUrl || constants_1.SITECORE_EDGE_URL_DEFAULT}/layout/component`, this.getComponentFetchParams(params));
|
|
55
|
+
}
|
|
75
56
|
}
|
|
76
57
|
exports.RestComponentLayoutService = RestComponentLayoutService;
|
|
@@ -316,12 +316,7 @@ export class SitecoreClient {
|
|
|
316
316
|
return new GraphQLErrorPagesService(Object.assign(Object.assign({}, this.initOptions), { language: this.initOptions.defaultLanguage, clientFactory: this.clientFactory }));
|
|
317
317
|
}
|
|
318
318
|
getComponentService() {
|
|
319
|
-
|
|
320
|
-
return new RestComponentLayoutService({
|
|
321
|
-
apiHost: (_a = this.initOptions.api.local) === null || _a === void 0 ? void 0 : _a.apiHost,
|
|
322
|
-
apiKey: (_b = this.initOptions.api.local) === null || _b === void 0 ? void 0 : _b.apiKey,
|
|
323
|
-
siteName: this.initOptions.defaultSite,
|
|
324
|
-
});
|
|
319
|
+
return new RestComponentLayoutService(this.initOptions.api.edge);
|
|
325
320
|
}
|
|
326
321
|
getSitePathService() {
|
|
327
322
|
return new GraphQLSitePathService({
|
|
@@ -1,44 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { NativeDataFetcher } from '../native-fetcher';
|
|
2
|
+
import debug from '../debug';
|
|
3
|
+
import { SITECORE_EDGE_URL_DEFAULT } from '../constants';
|
|
4
|
+
import { resolveUrl } from '../utils';
|
|
3
5
|
/**
|
|
4
|
-
* REST service that enables
|
|
5
|
-
* Makes a request to /sitecore/api/layout/component in 'library' mode in Pages.
|
|
6
|
+
* REST service that enables design Library functionality
|
|
6
7
|
* Returns layoutData for one single rendered component
|
|
7
8
|
*/
|
|
8
9
|
export class RestComponentLayoutService {
|
|
9
10
|
constructor(config) {
|
|
10
11
|
this.config = config;
|
|
11
|
-
this.getFetcher = (req, res) => {
|
|
12
|
-
return this.config.dataFetcherResolver
|
|
13
|
-
? this.config.dataFetcherResolver(req, res)
|
|
14
|
-
: this.getDefaultFetcher(req);
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* Provides default @see NativeDataFetcher data fetcher
|
|
18
|
-
* @param {IncomingMessage} [req] Request instance
|
|
19
|
-
* @returns default fetcher
|
|
20
|
-
*/
|
|
21
|
-
this.getDefaultFetcher = (req) => {
|
|
22
|
-
var _a;
|
|
23
|
-
const config = {
|
|
24
|
-
debugger: debug.editing,
|
|
25
|
-
};
|
|
26
|
-
const nativeFetcher = new NativeDataFetcher(config);
|
|
27
|
-
const headers = req && Object.assign(Object.assign({}, req.headers), (((_a = req.socket) === null || _a === void 0 ? void 0 : _a.remoteAddress) ? { 'X-Forwarded-For': req.socket.remoteAddress } : {}));
|
|
28
|
-
const fetcher = (url, data) => {
|
|
29
|
-
data = Object.assign(Object.assign({}, data), { headers: headers });
|
|
30
|
-
return nativeFetcher.fetch(url, data);
|
|
31
|
-
};
|
|
32
|
-
return fetcher;
|
|
33
|
-
};
|
|
34
12
|
}
|
|
35
|
-
fetchComponentData(params
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
debug.layout('fetching component with uid %s for %s %s %s', params.componentUid, params.itemId, params.language, params.siteName);
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
13
|
+
fetchComponentData(params) {
|
|
14
|
+
const config = { debugger: debug.layout };
|
|
15
|
+
const fetcher = new NativeDataFetcher(config);
|
|
16
|
+
debug.layout('fetching component with uid %s for %s %s %s %s', params.componentUid, params.itemId, params.language, params.siteName, params.dataSourceId);
|
|
17
|
+
const fetchUrl = this.getFetchUrl(params);
|
|
18
|
+
return fetcher
|
|
19
|
+
.get(fetchUrl)
|
|
20
|
+
.then((response) => response.data)
|
|
21
|
+
.catch((error) => {
|
|
42
22
|
var _a;
|
|
43
23
|
if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 404) {
|
|
44
24
|
return error.response.data;
|
|
@@ -46,19 +26,10 @@ export class RestComponentLayoutService {
|
|
|
46
26
|
throw error;
|
|
47
27
|
});
|
|
48
28
|
}
|
|
49
|
-
/**
|
|
50
|
-
* Resolves layout service url
|
|
51
|
-
* @param {string} apiType which layout service API to call ('render' or 'placeholder')
|
|
52
|
-
* @returns the layout service url
|
|
53
|
-
*/
|
|
54
|
-
resolveLayoutServiceUrl(apiType) {
|
|
55
|
-
const { apiHost = '', configurationName = 'jss' } = this.config;
|
|
56
|
-
return `${apiHost}/sitecore/api/layout/${apiType}/${configurationName}`;
|
|
57
|
-
}
|
|
58
29
|
getComponentFetchParams(params) {
|
|
59
30
|
// exclude undefined params with this one simple trick
|
|
60
31
|
return JSON.parse(JSON.stringify({
|
|
61
|
-
|
|
32
|
+
sitecoreContextId: this.config.contextId,
|
|
62
33
|
item: params.itemId,
|
|
63
34
|
uid: params.componentUid,
|
|
64
35
|
dataSourceId: params.dataSourceId,
|
|
@@ -66,7 +37,14 @@ export class RestComponentLayoutService {
|
|
|
66
37
|
version: params.version,
|
|
67
38
|
sc_site: params.siteName,
|
|
68
39
|
sc_lang: params.language || 'en',
|
|
69
|
-
sc_mode: params.editMode,
|
|
70
40
|
}));
|
|
71
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Get the fetch URL for the partial layout data endpoint
|
|
44
|
+
* @param {ComponentLayoutRequestParams} params - The parameters for the request
|
|
45
|
+
* @returns {string} The fetch URL for the component data
|
|
46
|
+
*/
|
|
47
|
+
getFetchUrl(params) {
|
|
48
|
+
return resolveUrl(`${this.config.edgeUrl || SITECORE_EDGE_URL_DEFAULT}/layout/component`, this.getComponentFetchParams(params));
|
|
49
|
+
}
|
|
72
50
|
}
|
|
@@ -27,6 +27,7 @@ export class GraphQLRobotsService {
|
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
29
|
* Fetch a data of robots.txt from API
|
|
30
|
+
* @param {FetchOptions} fetchOptions - The fetch options to be used for the request.
|
|
30
31
|
* @returns text of robots.txt
|
|
31
32
|
* @throws {Error} if the siteName is empty.
|
|
32
33
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sitecore-content-sdk/core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1-canary.2",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -77,7 +77,7 @@
|
|
|
77
77
|
},
|
|
78
78
|
"description": "",
|
|
79
79
|
"types": "types/index.d.ts",
|
|
80
|
-
"gitHead": "
|
|
80
|
+
"gitHead": "d99cf177d6e14d8adfb0f2ae5f50a2563cebe679",
|
|
81
81
|
"files": [
|
|
82
82
|
"dist",
|
|
83
83
|
"types",
|
|
@@ -1,41 +1,4 @@
|
|
|
1
|
-
import { LayoutServiceData
|
|
2
|
-
import { IncomingMessage, ServerResponse } from 'http';
|
|
3
|
-
import { HttpDataFetcher } from '../data-fetcher';
|
|
4
|
-
/**
|
|
5
|
-
* Data fetcher resolver in order to provide custom data fetcher
|
|
6
|
-
* @param {IncomingMessage} [req] Request instance
|
|
7
|
-
* @param {ServerResponse} [res] Response instance
|
|
8
|
-
*/
|
|
9
|
-
export type DataFetcherResolver = <T>(req?: IncomingMessage, res?: ServerResponse) => HttpDataFetcher<T>;
|
|
10
|
-
export type RestLayoutServiceConfig = {
|
|
11
|
-
/**
|
|
12
|
-
* Your Sitecore instance hostname that is the backend for JSS
|
|
13
|
-
*/
|
|
14
|
-
apiHost: string;
|
|
15
|
-
/**
|
|
16
|
-
* The Sitecore SSC API key your app uses
|
|
17
|
-
*/
|
|
18
|
-
apiKey: string;
|
|
19
|
-
/**
|
|
20
|
-
* The JSS application name
|
|
21
|
-
*/
|
|
22
|
-
siteName: string;
|
|
23
|
-
/**
|
|
24
|
-
* Enables/disables analytics tracking for the Layout Service invocation (default is true).
|
|
25
|
-
* More than likely, this would be set to false for SSG/hybrid implementations, and the
|
|
26
|
-
* JSS tracker would instead be used on the client-side
|
|
27
|
-
* @default true
|
|
28
|
-
*/
|
|
29
|
-
tracking?: boolean;
|
|
30
|
-
/**
|
|
31
|
-
* Function that handles fetching API data
|
|
32
|
-
*/
|
|
33
|
-
dataFetcherResolver?: DataFetcherResolver;
|
|
34
|
-
/**
|
|
35
|
-
* Layout Service "named" configuration
|
|
36
|
-
*/
|
|
37
|
-
configurationName?: string;
|
|
38
|
-
};
|
|
1
|
+
import { LayoutServiceData } from '../layout/models';
|
|
39
2
|
/**
|
|
40
3
|
* Params for requesting component data from service in Design Library mode
|
|
41
4
|
*/
|
|
@@ -66,35 +29,37 @@ export interface ComponentLayoutRequestParams {
|
|
|
66
29
|
*/
|
|
67
30
|
version?: string;
|
|
68
31
|
/**
|
|
69
|
-
*
|
|
32
|
+
* site name to be used as context for rendering the component
|
|
70
33
|
*/
|
|
71
|
-
|
|
34
|
+
siteName: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Config for the RestComponentLayoutService
|
|
38
|
+
*/
|
|
39
|
+
export interface RestComponentLayoutServiceConfig {
|
|
72
40
|
/**
|
|
73
|
-
*
|
|
41
|
+
* A unified identifier used to connect and retrieve data from XM Cloud instance
|
|
74
42
|
*/
|
|
75
|
-
|
|
43
|
+
contextId: string;
|
|
44
|
+
/**
|
|
45
|
+
* XM Cloud endpoint that the app will communicate and retrieve data from
|
|
46
|
+
* @default https://edge-platform.sitecorecloud.io
|
|
47
|
+
*/
|
|
48
|
+
edgeUrl?: string;
|
|
76
49
|
}
|
|
77
50
|
/**
|
|
78
|
-
* REST service that enables
|
|
79
|
-
* Makes a request to /sitecore/api/layout/component in 'library' mode in Pages.
|
|
51
|
+
* REST service that enables design Library functionality
|
|
80
52
|
* Returns layoutData for one single rendered component
|
|
81
53
|
*/
|
|
82
54
|
export declare class RestComponentLayoutService {
|
|
83
55
|
private config;
|
|
84
|
-
constructor(config:
|
|
85
|
-
fetchComponentData(params: ComponentLayoutRequestParams
|
|
86
|
-
protected
|
|
87
|
-
/**
|
|
88
|
-
* Resolves layout service url
|
|
89
|
-
* @param {string} apiType which layout service API to call ('render' or 'placeholder')
|
|
90
|
-
* @returns the layout service url
|
|
91
|
-
*/
|
|
92
|
-
protected resolveLayoutServiceUrl(apiType: string): string;
|
|
56
|
+
constructor(config: RestComponentLayoutServiceConfig);
|
|
57
|
+
fetchComponentData(params: ComponentLayoutRequestParams): Promise<LayoutServiceData>;
|
|
58
|
+
protected getComponentFetchParams(params: ComponentLayoutRequestParams): any;
|
|
93
59
|
/**
|
|
94
|
-
*
|
|
95
|
-
* @param {
|
|
96
|
-
* @returns
|
|
60
|
+
* Get the fetch URL for the partial layout data endpoint
|
|
61
|
+
* @param {ComponentLayoutRequestParams} params - The parameters for the request
|
|
62
|
+
* @returns {string} The fetch URL for the component data
|
|
97
63
|
*/
|
|
98
|
-
|
|
99
|
-
protected getComponentFetchParams(params: ComponentLayoutRequestParams): any;
|
|
64
|
+
private getFetchUrl;
|
|
100
65
|
}
|
|
@@ -33,13 +33,6 @@ export interface NativeDataFetcherResponse<T> {
|
|
|
33
33
|
export type NativeDataFetcherError = Error & {
|
|
34
34
|
response: NativeDataFetcherResponse<unknown>;
|
|
35
35
|
};
|
|
36
|
-
/**
|
|
37
|
-
* A function that fetches data from a given URL and returns a `NativeDataFetcherResponse`.
|
|
38
|
-
* @param {string} url The URL to request (can include query string parameters).
|
|
39
|
-
* @param {unknown} [data] Optional data to send with the request (e.g., for POST or PUT requests).
|
|
40
|
-
* @returns {Promise<NativeDataFetcherResponse<T>>} A promise that resolves to a `NativeDataFetcherResponse<T>`,
|
|
41
|
-
*/
|
|
42
|
-
export type NativeDataFetcherFunction<T> = (url: string, data?: RequestInit) => Promise<NativeDataFetcherResponse<T>>;
|
|
43
36
|
export type NativeDataFetcherConfig = NativeDataFetcherOptions & RequestInit;
|
|
44
37
|
export declare class NativeDataFetcher {
|
|
45
38
|
protected config: NativeDataFetcherConfig;
|
|
@@ -35,6 +35,7 @@ export declare class GraphQLRobotsService {
|
|
|
35
35
|
protected get query(): string;
|
|
36
36
|
/**
|
|
37
37
|
* Fetch a data of robots.txt from API
|
|
38
|
+
* @param {FetchOptions} fetchOptions - The fetch options to be used for the request.
|
|
38
39
|
* @returns text of robots.txt
|
|
39
40
|
* @throws {Error} if the siteName is empty.
|
|
40
41
|
*/
|
package/dist/cjs/data-fetcher.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ResponseError = void 0;
|
|
4
|
-
exports.fetchData = fetchData;
|
|
5
|
-
const utils_1 = require("./utils/utils");
|
|
6
|
-
class ResponseError extends Error {
|
|
7
|
-
constructor(message, response) {
|
|
8
|
-
super(message);
|
|
9
|
-
Object.setPrototypeOf(this, ResponseError.prototype);
|
|
10
|
-
this.response = response;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
exports.ResponseError = ResponseError;
|
|
14
|
-
/**
|
|
15
|
-
* @param {string} url the URL to request; may include query string
|
|
16
|
-
* @param {HttpDataFetcher<T> | NativeDataFetcherFunction<T>} fetcher the fetcher to use to perform the request
|
|
17
|
-
* @param {ParsedUrlQueryInput} params the query string parameters to send with the request
|
|
18
|
-
*/
|
|
19
|
-
async function fetchData(url, fetcher, params = {}) {
|
|
20
|
-
const response = await fetcher((0, utils_1.resolveUrl)(url, params));
|
|
21
|
-
return response.data;
|
|
22
|
-
}
|
package/dist/esm/data-fetcher.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { resolveUrl } from './utils/utils';
|
|
2
|
-
export class ResponseError extends Error {
|
|
3
|
-
constructor(message, response) {
|
|
4
|
-
super(message);
|
|
5
|
-
Object.setPrototypeOf(this, ResponseError.prototype);
|
|
6
|
-
this.response = response;
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* @param {string} url the URL to request; may include query string
|
|
11
|
-
* @param {HttpDataFetcher<T> | NativeDataFetcherFunction<T>} fetcher the fetcher to use to perform the request
|
|
12
|
-
* @param {ParsedUrlQueryInput} params the query string parameters to send with the request
|
|
13
|
-
*/
|
|
14
|
-
export async function fetchData(url, fetcher, params = {}) {
|
|
15
|
-
const response = await fetcher(resolveUrl(url, params));
|
|
16
|
-
return response.data;
|
|
17
|
-
}
|
package/types/data-fetcher.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { NativeDataFetcherFunction } from './native-fetcher';
|
|
2
|
-
import { ParsedUrlQueryInput } from 'querystring';
|
|
3
|
-
/**
|
|
4
|
-
* Response data for an HTTP request sent to an API
|
|
5
|
-
* @template T the type of data model requested
|
|
6
|
-
*/
|
|
7
|
-
export interface HttpResponse<T> {
|
|
8
|
-
/** HTTP status code of the response (i.e. 200, 404) */
|
|
9
|
-
status: number;
|
|
10
|
-
/** HTTP status text of the response (i.e. 'OK', 'Bad Request') */
|
|
11
|
-
statusText: string;
|
|
12
|
-
/** Response content */
|
|
13
|
-
data: T;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Describes functions that fetch data asynchronously (i.e. from an API endpoint).
|
|
17
|
-
* This interface conforms to 'fetch' public API, but is adaptable to other HTTP libraries and
|
|
18
|
-
* fetch polyfills.
|
|
19
|
-
* The interface implementation must:
|
|
20
|
-
* - Support SSR
|
|
21
|
-
* - Comply with the rules of REST by returning appropriate response status codes when there is an error instead of throwing exceptions.
|
|
22
|
-
* - Send HTTP POST requests if `data` param is specified; GET is suggested but not required for data-less requests
|
|
23
|
-
*/
|
|
24
|
-
export type HttpDataFetcher<T> = (url: string, data?: unknown) => Promise<HttpResponse<T>>;
|
|
25
|
-
export declare class ResponseError extends Error {
|
|
26
|
-
response: HttpResponse<unknown>;
|
|
27
|
-
constructor(message: string, response: HttpResponse<unknown>);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* @param {string} url the URL to request; may include query string
|
|
31
|
-
* @param {HttpDataFetcher<T> | NativeDataFetcherFunction<T>} fetcher the fetcher to use to perform the request
|
|
32
|
-
* @param {ParsedUrlQueryInput} params the query string parameters to send with the request
|
|
33
|
-
*/
|
|
34
|
-
export declare function fetchData<T>(url: string, fetcher: HttpDataFetcher<T> | NativeDataFetcherFunction<T>, params?: ParsedUrlQueryInput): Promise<T>;
|