Expand the '.env()' API call with more flexibility.
This commit is contained in:
parent
148e6dfc26
commit
2c3eb3096f
@ -1,19 +1,40 @@
|
||||
// @flow
|
||||
import type { CacheConfigurator, SimpleCacheConfigurator } from "../caching";
|
||||
|
||||
type EnvFunction = {
|
||||
(): string,
|
||||
<T>((string) => T): T,
|
||||
(string): boolean,
|
||||
(Array<string>): boolean,
|
||||
};
|
||||
|
||||
export type PluginAPI = {
|
||||
cache: SimpleCacheConfigurator,
|
||||
env: () => string,
|
||||
env: EnvFunction,
|
||||
async: () => boolean,
|
||||
};
|
||||
|
||||
export default function makeAPI(
|
||||
cache: CacheConfigurator<{ envName: string }>,
|
||||
): PluginAPI {
|
||||
const env: any = value =>
|
||||
cache.using(data => {
|
||||
if (typeof value === "undefined") return data.envName;
|
||||
if (typeof value === "function") return value(data.envName);
|
||||
if (!Array.isArray(value)) value = [value];
|
||||
|
||||
return value.some(entry => {
|
||||
if (typeof entry !== "string") {
|
||||
throw new Error("Unexpected non-string value");
|
||||
}
|
||||
return entry === data.envName;
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
cache: cache.simple(),
|
||||
// Expose ".env()" so people can easily get the same env that we expose using the "env" key.
|
||||
env: () => cache.using(data => data.envName),
|
||||
env,
|
||||
async: () => false,
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user