This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+82
View File
@@ -0,0 +1,82 @@
/// <reference path="../../adonis-typings/index.d.ts" />
import { AllowedSessionValues, StoreContract } from '@ioc:Adonis/Addons/Session';
/**
* Session store to mutate and access values from the session object
*/
export declare class Store implements StoreContract {
/**
* Underlying store values
*/
private values;
constructor(values: {
[key: string]: any;
} | null);
/**
* Find if store is empty or not
*/
get isEmpty(): boolean;
/**
* Set key/value pair
*/
set(key: string, value: AllowedSessionValues): void;
/**
* Get value for a given key
*/
get(key: string, defaultValue?: any): any;
/**
* Remove key
*/
unset(key: string): void;
/**
* Reset store by clearing it's values.
*/
clear(): void;
/**
* Pull value from the store. It is same as calling
* store.get and then store.unset
*/
pull(key: string, defaultValue?: any): any;
/**
* Increment number. The method raises an error when
* nderlying value is not a number
*/
increment(key: string, steps?: number): void;
/**
* Increment number. The method raises an error when
* nderlying value is not a number
*/
decrement(key: string, steps?: number): void;
/**
* Overwrite the underlying values object
*/
update(values: {
[key: string]: any;
}): void;
/**
* Update to merge values
*/
merge(values: {
[key: string]: any;
}): any;
/**
* A boolean to know if value exists. Extra guards to check
* arrays for it's length as well.
*/
has(key: string, checkForArraysLength?: boolean): boolean;
/**
* Get all values
*/
all(): any;
/**
* Returns object representation of values
*/
toObject(): any;
/**
* Returns the store values
*/
toJSON(): any;
/**
* Returns string representation of the store
*/
toString(): string;
}
+131
View File
@@ -0,0 +1,131 @@
"use strict";
/*
* @adonisjs/redis
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Store = void 0;
/// <reference path="../../adonis-typings/index.ts" />
const utils_1 = require("@poppinss/utils");
/**
* Session store to mutate and access values from the session object
*/
class Store {
constructor(values) {
this.values = values || {};
}
/**
* Find if store is empty or not
*/
get isEmpty() {
return !this.values || Object.keys(this.values).length === 0;
}
/**
* Set key/value pair
*/
set(key, value) {
utils_1.lodash.set(this.values, key, value);
}
/**
* Get value for a given key
*/
get(key, defaultValue) {
return utils_1.lodash.get(this.values, key, defaultValue);
}
/**
* Remove key
*/
unset(key) {
utils_1.lodash.unset(this.values, key);
}
/**
* Reset store by clearing it's values.
*/
clear() {
this.update({});
}
/**
* Pull value from the store. It is same as calling
* store.get and then store.unset
*/
pull(key, defaultValue) {
return ((value) => {
this.unset(key);
return value;
})(this.get(key, defaultValue));
}
/**
* Increment number. The method raises an error when
* nderlying value is not a number
*/
increment(key, steps = 1) {
const value = this.get(key, 0);
if (typeof value !== 'number') {
throw new utils_1.Exception(`Cannot increment "${key}", since original value is not a number`);
}
this.set(key, value + steps);
}
/**
* Increment number. The method raises an error when
* nderlying value is not a number
*/
decrement(key, steps = 1) {
const value = this.get(key, 0);
if (typeof value !== 'number') {
throw new utils_1.Exception(`Cannot increment "${key}", since original value is not a number`);
}
this.set(key, value - steps);
}
/**
* Overwrite the underlying values object
*/
update(values) {
this.values = values;
}
/**
* Update to merge values
*/
merge(values) {
utils_1.lodash.merge(this.values, values);
}
/**
* A boolean to know if value exists. Extra guards to check
* arrays for it's length as well.
*/
has(key, checkForArraysLength = true) {
const value = this.get(key);
if (!Array.isArray(value)) {
return !!value;
}
return checkForArraysLength ? value.length > 0 : !!value;
}
/**
* Get all values
*/
all() {
return this.values;
}
/**
* Returns object representation of values
*/
toObject() {
return this.all();
}
/**
* Returns the store values
*/
toJSON() {
return this.all();
}
/**
* Returns string representation of the store
*/
toString() {
return JSON.stringify(this.all());
}
}
exports.Store = Store;