83 lines
2.2 KiB
JavaScript
83 lines
2.2 KiB
JavaScript
/**
|
|
* API path generation utilities
|
|
*/
|
|
|
|
import { join } from './utils.js';
|
|
|
|
const _index_json = 'index.json';
|
|
const _excluded_paths = ['/404', '/_app', '/_error', '/sitemap.xml', '/_middleware'];
|
|
|
|
/**
|
|
* Get API path for a page
|
|
* @param {string} buildId - Build ID
|
|
* @param {string} basePath - Base path (optional)
|
|
* @param {string} path - Page path (optional)
|
|
* @returns {string|null} API path or null
|
|
*/
|
|
export function getApiPath(buildId, basePath = '', path = null) {
|
|
if (path === null) {
|
|
path = _index_json;
|
|
} else if (_excluded_paths.includes(path)) {
|
|
return null;
|
|
}
|
|
|
|
if (!path.endsWith('.json')) {
|
|
path += '.json';
|
|
}
|
|
|
|
if (path.endsWith('/.json')) {
|
|
path = _index_json;
|
|
}
|
|
|
|
return join(basePath, '/_next/data/', buildId, path);
|
|
}
|
|
|
|
/**
|
|
* Get index API path
|
|
* @param {string} buildId - Build ID
|
|
* @param {string} basePath - Base path (optional)
|
|
* @returns {string} Index API path
|
|
*/
|
|
export function getIndexApiPath(buildId, basePath = '') {
|
|
return getApiPath(buildId, basePath, _index_json);
|
|
}
|
|
|
|
/**
|
|
* Check if API is exposed from response
|
|
* @param {number} statusCode - HTTP status code
|
|
* @param {string} contentType - Content-Type header
|
|
* @param {string} text - Response text
|
|
* @returns {boolean} True if API is exposed
|
|
*/
|
|
export function isApiExposedFromResponse(statusCode, contentType, text) {
|
|
if (statusCode === 200) {
|
|
return true;
|
|
} else if (contentType !== null && contentType.startsWith('application/json')) {
|
|
return true;
|
|
} else {
|
|
return text === '{"notFound":true}';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* List API paths from build manifest
|
|
* @param {Array<string>} sortedPages - Sorted pages from build manifest
|
|
* @param {string} buildId - Build ID
|
|
* @param {string} basePath - Base path
|
|
* @param {boolean} isApiExposed - Is API exposed (optional)
|
|
* @returns {Array<string>} Array of API paths
|
|
*/
|
|
export function listApiPaths(sortedPages, buildId, basePath, isApiExposed = true) {
|
|
const result = [];
|
|
|
|
if (isApiExposed !== false) {
|
|
for (const path of sortedPages) {
|
|
const apiPath = getApiPath(buildId, basePath, path);
|
|
if (apiPath !== null) {
|
|
result.push(apiPath);
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|