@airgram/core
Advanced tools
Comparing version 2.3.0-next.14 to 2.3.0-next.16
@@ -98,23 +98,2 @@ "use strict"; | ||
} | ||
function defineContextProperty(ctx, name, value) { | ||
var descriptor = { | ||
enumerable: true, | ||
configurable: false | ||
}; | ||
if (typeof value === 'function') { | ||
descriptor.get = function () { | ||
return value(); | ||
}; | ||
} | ||
else if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') { | ||
descriptor.value = value; | ||
descriptor.writable = false; | ||
} | ||
else { | ||
descriptor.get = function () { | ||
return value; | ||
}; | ||
} | ||
Object.defineProperty(ctx, name, descriptor); | ||
} | ||
function isUnwrapped(o) { | ||
@@ -253,3 +232,3 @@ return typeof o !== 'function'; | ||
var _this = this; | ||
return this.createContext(request.method, (options && options.state) || {}, { request: request }).then(function (ctx) { return new Promise(function (resolve, reject) { | ||
return this.createContext(request.method, { options: options || {}, request: request }).then(function (ctx) { return new Promise(function (resolve, reject) { | ||
var handler = components_1.Composer.compose([_this.composer.middleware(), _this.apiMiddleware()]); | ||
@@ -260,25 +239,27 @@ return handler(ctx, function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { | ||
}); }); | ||
// return new Promise<any>((resolve, reject) => { | ||
// const handler = Composer.compose([this.composer.middleware(), this.apiMiddleware()]) | ||
// return handler(ctx, async (): Promise<any> => resolve(ctx)).catch(reject) | ||
// })// .catch((error) => this.handleError(error, ctx)) | ||
}; | ||
Airgram.prototype.createContext = function (_, state, props) { | ||
Airgram.prototype.createContext = function (_, props) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var ctx, extraContext; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
var ctx, descriptor, defineProperties, _c; | ||
var _this = this; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
ctx = createState(state); | ||
defineContextProperty(ctx, '_', _); | ||
defineContextProperty(ctx, 'airgram', this); | ||
Object.keys(props).forEach(function (name) { | ||
defineContextProperty(ctx, name, props[name]); | ||
}); | ||
ctx = {}; | ||
descriptor = { | ||
enumerable: true, | ||
configurable: false | ||
}; | ||
Object.defineProperty(ctx, 'airgram', __assign(__assign({}, descriptor), { get: function () { return _this; } })); | ||
defineProperties = function (obj) { return Object.keys(obj).forEach(function (name) { | ||
Object.defineProperty(ctx, name, __assign(__assign({}, descriptor), { value: obj[name], writable: false })); | ||
}); }; | ||
defineProperties(createState(((_b = (_a = props) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.state) || {})); | ||
defineProperties({ _: _ }); | ||
defineProperties(props); | ||
_c = defineProperties; | ||
return [4 /*yield*/, this.getExtraContext(ctx)]; | ||
case 1: | ||
extraContext = _a.sent(); | ||
Object.keys(extraContext).forEach(function (name) { | ||
defineContextProperty(ctx, name, extraContext[name]); | ||
}); | ||
_c.apply(void 0, [_d.sent()]); | ||
return [2 /*return*/, ctx]; | ||
@@ -309,3 +290,3 @@ } | ||
if (state === void 0) { state = {}; } | ||
return this.createContext(update._, state, { update: update }) | ||
return this.createContext(update._, { update: update, options: { state: state } }) | ||
.then(function (ctx) { return _this.composer.middleware()(ctx, components_1.Composer.noop); }); | ||
@@ -312,0 +293,0 @@ }; |
{ | ||
"name": "@airgram/core", | ||
"version": "2.3.0-next.14", | ||
"version": "2.3.0-next.16", | ||
"private": false, | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
}, | ||
"gitHead": "86116df943d40f17f3b5cdbaead57709ef1e2cf0", | ||
"gitHead": "4f9f43510e40f5266cf169665309c4fb691beaec", | ||
"sideEffects": false, | ||
@@ -14,0 +14,0 @@ "author": "Alexander Krisko", |
@@ -27,3 +27,3 @@ import * as api from './api'; | ||
provider: ProviderT; | ||
context?: ExtraContext | ((ctx: BaseContext) => ExtraContext) | ((ctx: BaseContext) => Promise<ExtraContext>); | ||
context?: ExtraContext | ((ctx: BaseApiResponse<any, any> | BaseUpdateContext<any>) => ExtraContext | Promise<ExtraContext>); | ||
models?: PlainObjectToModelTransformer; | ||
@@ -53,8 +53,11 @@ databaseEncryptionKey?: string; | ||
} | ||
export interface ApiResponse<ParamsT, ResultT extends BaseTdObject> extends ContextState { | ||
export interface BaseApiResponse<ParamsT, ResultT extends BaseTdObject> extends ContextState { | ||
_: Predicate<ResultT> | 'error'; | ||
request: ApiRequest<ParamsT>; | ||
response: ResultT | api.ErrorUnion; | ||
options: ApiRequestOptions; | ||
airgram: Instance; | ||
} | ||
export interface ApiResponse<ParamsT, ResultT extends BaseTdObject> extends BaseApiResponse<ParamsT, ResultT>, ExtraContext { | ||
} | ||
export interface TdProvider { | ||
@@ -77,3 +80,3 @@ destroy(): Promise<void>; | ||
} | ||
export interface UpdateContext<UpdateT extends BaseTdObject> extends ContextState { | ||
export interface BaseUpdateContext<UpdateT extends BaseTdObject> extends ContextState { | ||
_: Predicate<UpdateT>; | ||
@@ -83,6 +86,7 @@ update: UpdateT; | ||
} | ||
export declare type BaseContext = ApiResponse<unknown, TdObject> | UpdateContext<TdObject>; | ||
export interface UpdateContext<UpdateT extends BaseTdObject> extends BaseUpdateContext<UpdateT>, ExtraContext { | ||
} | ||
export declare type Context = ApiResponse<unknown, TdObject> | UpdateContext<TdObject>; | ||
export interface ExtraContext { | ||
} | ||
export declare type Context<ContextT extends BaseContext = BaseContext> = ContextT & ExtraContext; | ||
export {}; |
817398
18060