Comparing version 0.0.1-alpha.6 to 0.0.1
@@ -1,1 +0,39 @@ | ||
export declare const toCache: () => void; | ||
export declare type SetterFunc<K> = (key: string, value: K) => void; | ||
export declare type ResolverFunc<K> = (key: string) => Promise<K | undefined> | K | undefined; | ||
export declare type ResolverSetter<K> = { | ||
get: ResolverFunc<K>; | ||
set?: SetterFunc<K>; | ||
}; | ||
/** | ||
* Get data from different sources lineally and optionally update prior sources with data. | ||
* | ||
* @param key The key to get/set data for. | ||
* @param functions Array of resolvers. | ||
* | ||
* @returns The value for the key. | ||
* | ||
* @example | ||
* Example reading from multiple sources: | ||
* ```ts | ||
* const data = await useCache<OurReturnType>( | ||
'mykey', | ||
// Resolver with both getter and setter | ||
{ | ||
get: async (key) => { | ||
// Get the key from our local cache | ||
return await myLocalCache.get(key); | ||
}, | ||
set: async (key, value) => { | ||
// Set the key in our local cache | ||
await myLocalCache.set(key, value); | ||
}, | ||
}, | ||
// Resolver with only getter | ||
async (key) => { | ||
// Get the key from our remote cache | ||
return await myRemoteCache.get(key); | ||
} | ||
); | ||
* ``` | ||
*/ | ||
export declare const useCache: <K>(key: string, ...functions: (ResolverSetter<K> | ResolverFunc<K>)[]) => Promise<K | undefined>; |
113
lib/index.js
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toCache = void 0; | ||
var toCache = function () { | ||
console.log('Hello world'); | ||
exports.useCache = void 0; | ||
/** | ||
* Get data from different sources lineally and optionally update prior sources with data. | ||
* | ||
* @param key The key to get/set data for. | ||
* @param functions Array of resolvers. | ||
* | ||
* @returns The value for the key. | ||
* | ||
* @example | ||
* Example reading from multiple sources: | ||
* ```ts | ||
* const data = await useCache<OurReturnType>( | ||
'mykey', | ||
// Resolver with both getter and setter | ||
{ | ||
get: async (key) => { | ||
// Get the key from our local cache | ||
return await myLocalCache.get(key); | ||
}, | ||
set: async (key, value) => { | ||
// Set the key in our local cache | ||
await myLocalCache.set(key, value); | ||
}, | ||
}, | ||
// Resolver with only getter | ||
async (key) => { | ||
// Get the key from our remote cache | ||
return await myRemoteCache.get(key); | ||
} | ||
); | ||
* ``` | ||
*/ | ||
var useCache = function (key) { | ||
var functions = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
functions[_i - 1] = arguments[_i]; | ||
} | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var data, setters, _a, functions_1, ResolverData, _b, get, _c, set, _d, setters_1, set_1; | ||
return __generator(this, function (_e) { | ||
switch (_e.label) { | ||
case 0: | ||
setters = []; | ||
_a = 0, functions_1 = functions; | ||
_e.label = 1; | ||
case 1: | ||
if (!(_a < functions_1.length)) return [3 /*break*/, 4]; | ||
ResolverData = functions_1[_a]; | ||
_b = typeof ResolverData == 'function' | ||
? { get: ResolverData } | ||
: ResolverData, get = _b.get, _c = _b.set, set = _c === void 0 ? function () { } : _c; | ||
return [4 /*yield*/, get(key)]; | ||
case 2: | ||
data = _e.sent(); | ||
if (data !== undefined) { | ||
for (_d = 0, setters_1 = setters; _d < setters_1.length; _d++) { | ||
set_1 = setters_1[_d]; | ||
set_1(key, data); | ||
} | ||
return [3 /*break*/, 4]; | ||
} | ||
if (set) | ||
setters.push(set); | ||
_e.label = 3; | ||
case 3: | ||
_a++; | ||
return [3 /*break*/, 1]; | ||
case 4: return [2 /*return*/, data]; | ||
} | ||
}); | ||
}); | ||
}; | ||
exports.toCache = toCache; | ||
exports.useCache = useCache; |
@@ -23,3 +23,3 @@ { | ||
}, | ||
"version": "0.0.1-alpha.6", | ||
"version": "0.0.1", | ||
"scripts": { | ||
@@ -26,0 +26,0 @@ "build": "tsc", |
@@ -18,3 +18,3 @@ <p align="center"> | ||
A simplified general-purpose permissions system for node apps. | ||
Something with cache ig. Utilities for Caching / Data Fallback Handling. | ||
@@ -25,4 +25,4 @@ ## Table of Contents | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [Documentation](#documentation) | ||
- [useCache](#usecache) | ||
- [Contributors](#contributors) | ||
@@ -53,7 +53,35 @@ - [LICENSE](#license) | ||
Empty... for now | ||
### useCache | ||
useCache takes in as it's first parameter the key that will be sent to getter and setter functions to retrieve/store data. | ||
Further arguments will be used in order to determine how the data is stored and retrieved. | ||
In the example bellow, myLocalCache and myRemoteCache are imaginary caches that we use to demonstrate the function. | ||
```ts | ||
const data = await useCache<OurReturnType>( | ||
'myKey', | ||
// Resolver with both getter and setter | ||
{ | ||
get: async (key) => { | ||
// Get the key from our local cache | ||
return await myLocalCache.get(key); | ||
}, | ||
set: async (key, value) => { | ||
// Set the key in our local cache | ||
await myLocalCache.set(key, value); | ||
}, | ||
}, | ||
// Resolver with only getter | ||
async (key) => { | ||
// Get the key from our remote cache | ||
return await myRemoteCache.get(key); | ||
} | ||
); | ||
``` | ||
## Contributors | ||
[![](https://contrib.rocks/image?repo=lvkdotsh/error-fns)](https://github.com/lvkdotsh/error-fns/graphs/contributors) | ||
[![](https://contrib.rocks/image?repo=lvkdotsh/cache-fns)](https://github.com/lvkdotsh/cache-fns/graphs/contributors) | ||
@@ -60,0 +88,0 @@ ## LICENSE |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
10238
151
87
0