Expand the '.env()' API call with more flexibility.
This commit is contained in:
parent
148e6dfc26
commit
2c3eb3096f
@ -1,19 +1,40 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import type { CacheConfigurator, SimpleCacheConfigurator } from "../caching";
|
import type { CacheConfigurator, SimpleCacheConfigurator } from "../caching";
|
||||||
|
|
||||||
|
type EnvFunction = {
|
||||||
|
(): string,
|
||||||
|
<T>((string) => T): T,
|
||||||
|
(string): boolean,
|
||||||
|
(Array<string>): boolean,
|
||||||
|
};
|
||||||
|
|
||||||
export type PluginAPI = {
|
export type PluginAPI = {
|
||||||
cache: SimpleCacheConfigurator,
|
cache: SimpleCacheConfigurator,
|
||||||
env: () => string,
|
env: EnvFunction,
|
||||||
async: () => boolean,
|
async: () => boolean,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function makeAPI(
|
export default function makeAPI(
|
||||||
cache: CacheConfigurator<{ envName: string }>,
|
cache: CacheConfigurator<{ envName: string }>,
|
||||||
): PluginAPI {
|
): 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 {
|
return {
|
||||||
cache: cache.simple(),
|
cache: cache.simple(),
|
||||||
// Expose ".env()" so people can easily get the same env that we expose using the "env" key.
|
// 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,
|
async: () => false,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user