edgespec
Version:
Write Winter-CG compatible routes with filesystem routing and tons of features
122 lines (117 loc) • 3.79 kB
JavaScript
;
var path = require('path');
var fs = require('fs/promises');
var bundleRequire = require('bundle-require');
var zod = require('zod');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var path__default = /*#__PURE__*/_interopDefault(path);
var fs__default = /*#__PURE__*/_interopDefault(fs);
// src/config/utils.ts
var cloneObjectAndDeleteUndefinedKeys = (obj) => {
const clone = { ...obj };
Object.keys(clone).forEach((key) => {
if (clone[key] === void 0) {
delete clone[key];
}
});
return clone;
};
var resolvePossibleRelativePath = (possibleRelativePath, configDirectory) => {
if (path__default.default.isAbsolute(possibleRelativePath)) {
return possibleRelativePath;
}
return path__default.default.resolve(configDirectory, possibleRelativePath);
};
var resolveConfig = (config) => {
const { rootDirectory, tsconfigPath, routesDirectory, ...rest } = cloneObjectAndDeleteUndefinedKeys(config);
const resolvedRootDirectory = path__default.default.resolve(config.rootDirectory);
return {
rootDirectory: resolvedRootDirectory,
tsconfigPath: resolvePossibleRelativePath(
tsconfigPath ?? "tsconfig.json",
resolvedRootDirectory
),
routesDirectory: resolvePossibleRelativePath(
routesDirectory ?? "api",
resolvedRootDirectory
),
platform: "wintercg-minimal",
...rest
};
};
var validateConfig = async (config) => {
try {
await fs__default.default.stat(config.routesDirectory);
} catch (error) {
throw new Error(`Could not find routes directory ${config.routesDirectory}`);
}
try {
await fs__default.default.stat(config.tsconfigPath);
} catch (error) {
throw new Error(`Could not find tsconfig.json at ${config.tsconfigPath}`);
}
return config;
};
var loadConfig = async (rootDirectory, overrides) => {
let loadedConfig = {};
let configInRootExists = false;
const potentialConfigPath = path__default.default.join(rootDirectory, "edgespec.config.ts");
try {
await fs__default.default.stat(potentialConfigPath);
configInRootExists = true;
} catch {
}
if (configInRootExists) {
const {
mod: { default: config }
} = await bundleRequire.bundleRequire({
filepath: potentialConfigPath
});
if (!config) {
throw new Error(
`Could not find a default export in ${potentialConfigPath}`
);
}
loadedConfig = config;
}
return await validateConfig(
resolveConfig({
rootDirectory,
...loadedConfig,
...cloneObjectAndDeleteUndefinedKeys(overrides ?? {})
})
);
};
var edgeSpecConfigSchema = zod.z.object({
/**
* Defaults to the current working directory.
*/
rootDirectory: zod.z.string().optional(),
/**
* If this path is relative, it's resolved relative to the `rootDirectory` option.
*/
tsconfigPath: zod.z.string().optional(),
/**
* If this path is relative, it's resolved relative to the `rootDirectory` option.
*/
routesDirectory: zod.z.string().optional(),
/**
* The platform you're targeting.
*
* Defaults to `wintercg-minimal`, and you should use this whenever possible for maximal compatibility.
*
* Check [the docs](https://github.com/seamapi/edgespec/blob/main/docs/edgespec-config.md) for more information.
*/
platform: zod.z.enum(["node", "wintercg-minimal"]).default("wintercg-minimal").optional()
}).strict();
var defineConfig = (config) => {
const parsedConfig = edgeSpecConfigSchema.safeParse(config);
if (parsedConfig.success) {
return parsedConfig.data;
}
throw new Error(`Invalid config: ${parsedConfig.error}`);
};
exports.defineConfig = defineConfig;
exports.loadConfig = loadConfig;
//# sourceMappingURL=index.cjs.map
//# sourceMappingURL=index.cjs.map