-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathdata.js
151 lines (130 loc) · 4.4 KB
/
data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2021 Apple Inc. and the Swift project authors
* Licensed under Apache License v2.0 with Runtime Library Exception
*
* See https://swift.org/LICENSE.txt for license information
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import { normalizePath } from 'docc-render/utils/assets';
import {
queryStringForParams, areEquivalentLocations, getAbsoluteUrl,
} from 'docc-render/utils/url-helper';
import { getSetting } from 'docc-render/utils/theme-settings';
import emitWarningForSchemaVersionMismatch from 'docc-render/utils/schema-version-check';
import RedirectError from 'docc-render/errors/RedirectError';
import FetchError from 'docc-render/errors/FetchError';
export async function fetchData(path, params = {}, options = {}) {
function isBadResponse(response) {
// When this is running in an IDE target, the `fetch` API will be used with
// custom URL schemes. Right now, WebKit will return successful responses
// with an HTTP status of `0`, which is normally not considered an "OK"
// response, so this needs to be special cased as a good response here.
// Otherwise, the `ok` property will return true for any http status within
// the range of 200-299
if (process.env.VUE_APP_TARGET === 'ide' && response.status === 0) {
return false;
}
return !response.ok;
}
const url = getAbsoluteUrl(path);
const queryString = queryStringForParams(params);
if (queryString) {
url.search = queryString;
}
const response = await fetch(url.href, options);
if (isBadResponse(response)) {
throw response;
}
// check if there was a redirect and `next` exists
if (response.redirected) {
throw new RedirectError({
location: response.url,
response,
});
}
const json = await response.json();
emitWarningForSchemaVersionMismatch(json.schemaVersion);
return json;
}
function createDataPath(path) {
function filePathFor(path) {
if (process.env.VUE_APP_TARGET !== 'ide' &&
getSetting(['features', 'docs', 'portablePaths', 'enable'], false)) {
return path.replace(/\/$/, "").replace(/[<>:"\/\\|*]/, "_");
} else {
return path.replace(/\/$/, "")
}
}
return `${normalizePath(['/data', filePathFor(path)])}.json`;
}
/**
* Transforms a data JSON path, to a route path
* @param {string} dataURL - the JSON path
* @returns {string}
*/
function transformDataPathToRoutePath(dataURL) {
const { pathname, search } = new URL(dataURL);
const RE = /\/data(\/.*).json$/;
const match = RE.exec(pathname);
if (!match) return pathname + search;
return match[1] + search;
}
export async function fetchDataForRouteEnter(to, from, next) {
const path = createDataPath(to.path);
let data;
try {
data = await fetchData(path, to.query);
} catch (error) {
if (process.env.VUE_APP_TARGET === 'ide') {
console.error(error);
// We need to throw false to pass false to the following `catch` function
// so we stop the navigation by calling `next(false)`
/* eslint-disable no-throw-literal */
throw false;
}
if (error instanceof RedirectError) {
// throw the redirect location, so it's passed to the `next` error handler and
// vue router redirects to that location
throw transformDataPathToRoutePath(error.location);
}
if (error.status && error.status === 404) {
// route to 404 page if missing data, but not in IDE build
next({
name: 'not-found',
params: [to.path],
});
} else {
next(new FetchError(to));
}
}
return data;
}
export function shouldFetchDataForRouteUpdate(to, from) {
return !areEquivalentLocations(to, from);
}
export async function fetchAPIChangesForRoute(route, changes) {
const path = createDataPath(`/diffs${route.path}`);
let data;
try {
data = await fetchData(path, {
...route.query,
changes,
});
} catch (error) {
throw new Error(`Unable to fetch API changes: ${error}`);
}
return data;
}
export async function fetchDataForPreview(path, options = {}) {
const dataPath = createDataPath(path);
return fetchData(dataPath, {}, options);
}
export function clone(jsonObject) {
return JSON.parse(JSON.stringify(jsonObject));
}
export async function fetchIndexPathsData({ slug }) {
const path = getAbsoluteUrl(['/index/', slug, 'index.json']);
return fetchData(path);
}