Files
DefinitelyTyped/types/fast-als/index.d.ts
Elias 73067b6177 🤖 Merge PR #60214 fast-als typing by @elias-fauser
* Adding basic @types/pg-cursor

* Correcting header to requirements

* Trying further to meet requirement which I cannot test locally

I have neither the disk space nor the hours in the day to
run `npm run test-all` on a repository this large. Sorry I
have to iterate upon this using the remote CI.

* Is it an order problem, def-by before def?

* One down, another pops up

* Trying `export =` syntax, per hidden style guide

* Trying to deal with version differences

* Why are all of these checks in `test-all` and not in `test`?

* Add config and cursor typings to test

* Change pg import

* Change tslint.json to @definitelytyped

* Fix errors in pg-cursor

* pg-cursor: Add missing base class EventEmitter

* pg-cursor: Fix wrong return annotations and add deprecated functions

* pg-cursor: Fix linting errors

* pg-cursor: remove 'Edited by' definition

* Add fast-als typings

* Remove node reference

Co-authored-by: Tiogshi Laj <tiogshi@gmail.com>
Co-authored-by: Elias Fauser <e.fauser@sprinteins.com>
2022-05-06 06:58:37 -07:00

116 lines
3.4 KiB
TypeScript

// Type definitions for fast-als 1.0
// Project: https://github.com/thorough-developer/fast-als
// Definitions by: Elias Fauser <https://github.com/elias-fauser>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export {};
interface Context {
[key: string]: any;
}
type CallbackFunction = () => void | PromiseLike<void>;
type ReturnValue = string | number | boolean | symbol | object;
/**
* The start of creating an asynchronous local storage context. Once this method is called, a new context is created
* where get and set calls will set and return values as expected.
*
* @param defaults - Sets the default values. A convenience so that you don't have to check to see if a value is there and set it to something else.
* @param callback - This is the code to be executed first within the new context that is created
*
* @example
* const fastAls = require('fast-als');
*
* function firstCallInScope() {
* // override user
* fastAls.set('user', { id: 'overwrittenUser'});
* }
*
* function secondCallInScope() {
* // will print the user set in firstCallInScope
* console.log(fastAls.get('user'));
* }
*
* fastAls.runWith({ user: { id: 'someUser' } }, () => {
* firstCallInScope();
* secondCallInScope();
* });
*/
export function runWith(defaults: Context, callback: CallbackFunction): void;
/**
* Sets a variable for a given key within running context (started by runWith).
* If this is called outside of a running context, it will not store the value.
* @param key - the key to store the variable by
* @param value - the value to store under the key for lookup later on.
*
* @example
*
* const fastAls = require('fast-als');
*
* function callInScope() {
* // override user
* fastAls.set('user', { id: 'overwrittenUser'});
* }
*
* fastAls.runWith({ user: { id: 'someUser' } }, () => {
* callInScope();
* });
*
* @example
*
* const fastAls = require('fast-als');
*
* function callOutOfScope() {
* // this never gets set
* fastAls.set('user', { id: 'overwrittenUser'});
* }
*
* // calling this won't store the variable under the key
* callOutOfScope();
*/
export function set<Payload extends Context, Key extends keyof Payload>(key: Key, value: Payload[Key]): void;
export function set<Payload extends Context>(key: keyof Payload, value: Payload[typeof key]): void;
export function set(key: string, value: any): void;
/**
* Gets a variable previously set within a running context (started by runWith).
* If this is called outside of a running context, it will not retrieve the value.
*
* @param key - the key to retrieve the stored value
*
* @example
*
* const fastAls = require('fast-als');
*
* function callInScope() {
* // prints default user
* console.log(fastAls.get('user'));
* }
*
* fastAls.runWith({ user: { id: 'someUser' } }, () => {
* callInScope();
* });
*
* @example
*
* const fastAls = require('fast-als');
*
* function callInScope() {
* // prints default user
* console.log(fastAls.get('user'));
* }
*
* fastAls.runWith({ user: { id: 'someUser' } }, () => {
* callInScope();
* });
*
* // calling this will return undefined
* callInScope();
*/
export function get<Payload extends Context, Key extends keyof Payload>(key: Key): Payload[Key] | undefined;
export function get<Payload extends Context>(key: keyof Payload): Payload[typeof key] | undefined;
export function get(key: string): ReturnValue | undefined;