From 4b97d869087bbd87bdfdf58886a6bb242266c207 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Sun, 13 Aug 2023 10:55:39 -0500 Subject: [PATCH] chore: upgrade ts-results-es (#322) --- package.json | 2 +- src/core/ioc/dependency-injection.ts | 7 +---- src/core/operators.ts | 8 +++--- src/core/structures/context.ts | 42 ++++++++++++++++++++-------- src/core/structures/core-context.ts | 4 +-- src/handlers/event-utils.ts | 6 ++-- test/core/module-loading.test.ts | 1 - test/handlers/id.test.ts | 2 +- yarn.lock | 10 +++---- 9 files changed, 48 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index 722a4d8..58598c5 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "dependencies": { "iti": "^0.6.0", "rxjs": "^7.8.0", - "ts-results-es": "latest" + "ts-results-es": "^4.0.0" }, "devDependencies": { "@faker-js/faker": "^8.0.1", diff --git a/src/core/ioc/dependency-injection.ts b/src/core/ioc/dependency-injection.ts index a61e160..7a67de5 100644 --- a/src/core/ioc/dependency-injection.ts +++ b/src/core/ioc/dependency-injection.ts @@ -66,12 +66,7 @@ export async function composeRoot( } //Build the container based on the callback provided by the user conf.build(container as CoreContainer>); - try { - container.get('@sern/client'); - } catch { - throw new Error(SernError.MissingRequired + ' No client was provided'); - } - + if (!hasLogger) { container.get('@sern/logger')?.info({ message: 'All dependencies loaded successfully.' }); } diff --git a/src/core/operators.ts b/src/core/operators.ts index 9627b81..3953eac 100644 --- a/src/core/operators.ts +++ b/src/core/operators.ts @@ -52,7 +52,7 @@ export const arrayifySource = map(src => (Array.isArray(src) ? (src as unknown[] * Checks if the stream of results is all ok. */ export const everyPluginOk: OperatorFunction = pipe( - every(result => result.ok), + every(result => result.isOk()), defaultIfEmpty(true), ); @@ -74,10 +74,10 @@ export function handleError(crashHandler: ErrorHandling, logging?: Logging) { export const filterTap = (onErr: (e: R) => void): OperatorFunction, K> => pipe( concatMap(result => { - if(result.ok) { - return of(result.val) + if(result.isOk()) { + return of(result.value) } - onErr(result.val); + onErr(result.error); return EMPTY }) diff --git a/src/core/structures/context.ts b/src/core/structures/context.ts index 0fdc7f2..a009f90 100644 --- a/src/core/structures/context.ts +++ b/src/core/structures/context.ts @@ -31,15 +31,21 @@ export class Context extends CoreContext { } public get id(): Snowflake { - return this.ctx.val.id; + return safeUnwrap(this.ctx + .map(m => m.id) + .mapErr(i => i.id)); } public get channel() { - return this.ctx.val.channel; + return safeUnwrap(this.ctx + .map(m => m.channel) + .mapErr(i => i.channel)); } public get channelId(): Snowflake { - return safeUnwrap(this.ctx.map(m => m.channelId).mapErr(i => i.channelId)); + return safeUnwrap(this.ctx + .map(m => m.channelId) + .mapErr(i => i.channelId)); } /** @@ -47,7 +53,9 @@ export class Context extends CoreContext { * else, interaction.user */ public get user(): User { - return safeUnwrap(this.ctx.map(m => m.author).mapErr(i => i.user)); + return safeUnwrap(this.ctx + .map(m => m.author) + .mapErr(i => i.user)); } public get userId(): Snowflake { @@ -55,29 +63,41 @@ export class Context extends CoreContext { } public get createdTimestamp(): number { - return this.ctx.val.createdTimestamp; + return safeUnwrap(this.ctx + .map(m => m.createdTimestamp) + .mapErr(i => i.createdTimestamp)); } public get guild() { - return this.ctx.val.guild; + return safeUnwrap(this.ctx + .map(m => m.guild) + .mapErr(i => i.guild)); } public get guildId() { - return this.ctx.val.guildId; + return safeUnwrap(this.ctx + .map(m => m.guildId) + .mapErr(i => i.guildId)); } /* * interactions can return APIGuildMember if the guild it is emitted from is not cached */ public get member() { - return this.ctx.val.member; + return safeUnwrap(this.ctx + .map(m => m.member) + .mapErr(i => i.member)); } public get client(): Client { - return this.ctx.val.client; + return safeUnwrap(this.ctx + .map(m => m.client) + .mapErr(i => i.client)); } public get inGuild(): boolean { - return this.ctx.val.inGuild(); + return safeUnwrap(this.ctx + .map(m => m.inGuild()) + .mapErr(i => i.inGuild())); } public async reply(content: ReplyOptions) { @@ -100,5 +120,5 @@ export class Context extends CoreContext { } function safeUnwrap(res: Result) { - return res.val; + return res.unwrap() } diff --git a/src/core/structures/core-context.ts b/src/core/structures/core-context.ts index 08a0e78..c2e483c 100644 --- a/src/core/structures/core-context.ts +++ b/src/core/structures/core-context.ts @@ -7,7 +7,7 @@ import * as assert from 'node:assert'; */ export abstract class CoreContext { protected constructor(protected ctx: Either) { - assert.ok(typeof ctx.val === 'object' && ctx.val != null); + assert.ok(typeof ctx === 'object' && ctx != null); } get message(): M { return this.ctx.expect(SernError.MismatchEvent); @@ -17,7 +17,7 @@ export abstract class CoreContext { } public isMessage(): this is CoreContext { - return this.ctx.ok; + return this.ctx.isOk(); } public isSlash(): this is CoreContext { diff --git a/src/handlers/event-utils.ts b/src/handlers/event-utils.ts index bb8f82e..8e5c8cf 100644 --- a/src/handlers/event-utils.ts +++ b/src/handlers/event-utils.ts @@ -150,11 +150,11 @@ export function executeModule( //converting the task into a promise so rxjs can resolve the Awaitable properly concatMap(() => Result.wrapAsync(async () => task())), concatMap(result => { - if (result.ok) { + if (result.isOk()) { emitter.emit('module.activate', SernEmitter.success(module)); return EMPTY; } else { - return throwError(() => SernEmitter.failure(module, result.val)); + return throwError(() => SernEmitter.failure(module, result.error)); } }), ); @@ -182,7 +182,7 @@ export function createResultResolver< const task$ = config.createStream(args); return task$.pipe( tap(result => { - result.err && config.onStop?.(args.module); + result.isErr() && config.onStop?.(args.module); }), everyPluginOk, filterMapTo(() => config.onNext(args)), diff --git a/test/core/module-loading.test.ts b/test/core/module-loading.test.ts index 6e19d74..dc4e9a8 100644 --- a/test/core/module-loading.test.ts +++ b/test/core/module-loading.test.ts @@ -8,7 +8,6 @@ describe('module-loading', () => { const filename = Files.fmtFileName(name+'.'+extension); expect(filename).toBe(name) }) - // todo: handle commands with multiple extensions // it('should properly extract filename from file, nested multiple', () => { diff --git a/test/handlers/id.test.ts b/test/handlers/id.test.ts index e6a2ed8..a472742 100644 --- a/test/handlers/id.test.ts +++ b/test/handlers/id.test.ts @@ -18,7 +18,7 @@ function createRandomCommandModules() { CommandType.Button, ]; return commandModule({ - type: randomCommandType[Math.floor(Math.random() * randomCommandType.length)], + type: faker.helpers.uniqueArray(randomCommandType, 1)[0], description: faker.string.alpha(), name: faker.string.alpha(), execute: () => {}, diff --git a/yarn.lock b/yarn.lock index ed8a257..1562113 100644 --- a/yarn.lock +++ b/yarn.lock @@ -619,7 +619,7 @@ __metadata: iti: ^0.6.0 prettier: 2.8.8 rxjs: ^7.8.0 - ts-results-es: latest + ts-results-es: ^4.0.0 tsup: ^6.7.0 typescript: 5.0.2 vitest: latest @@ -3844,10 +3844,10 @@ __metadata: languageName: node linkType: hard -"ts-results-es@npm:latest": - version: 3.6.1 - resolution: "ts-results-es@npm:3.6.1" - checksum: af0d93ee4d3bd9e99a5fd4ac4b0ad090aef0a61e1f38ee596cfebe8d47090b34a2557d3778e00b4aae7c74962133805275ffffe56716e4d747fa559a926d9ced +"ts-results-es@npm:^4.0.0": + version: 4.0.0 + resolution: "ts-results-es@npm:4.0.0" + checksum: 32a7059491e36d06c5a1084fe9be8021a0beb2d94a94b0c3fa85dc3e96561bf34fb8fd60ebe661064c9fc2bafcf437b6b65f119e8d7497af7f76cda9d9a2a945 languageName: node linkType: hard