chore: upgrade ts-results-es (#322)

This commit is contained in:
Jacob Nguyen
2023-08-13 10:55:39 -05:00
committed by GitHub
parent b1c82448bd
commit 4b97d86908
9 changed files with 48 additions and 34 deletions

View File

@@ -40,7 +40,7 @@
"dependencies": { "dependencies": {
"iti": "^0.6.0", "iti": "^0.6.0",
"rxjs": "^7.8.0", "rxjs": "^7.8.0",
"ts-results-es": "latest" "ts-results-es": "^4.0.0"
}, },
"devDependencies": { "devDependencies": {
"@faker-js/faker": "^8.0.1", "@faker-js/faker": "^8.0.1",

View File

@@ -66,11 +66,6 @@ export async function composeRoot(
} }
//Build the container based on the callback provided by the user //Build the container based on the callback provided by the user
conf.build(container as CoreContainer<Omit<CoreDependencies, '@sern/client'>>); conf.build(container as CoreContainer<Omit<CoreDependencies, '@sern/client'>>);
try {
container.get('@sern/client');
} catch {
throw new Error(SernError.MissingRequired + ' No client was provided');
}
if (!hasLogger) { if (!hasLogger) {
container.get('@sern/logger')?.info({ message: 'All dependencies loaded successfully.' }); container.get('@sern/logger')?.info({ message: 'All dependencies loaded successfully.' });

View File

@@ -52,7 +52,7 @@ export const arrayifySource = map(src => (Array.isArray(src) ? (src as unknown[]
* Checks if the stream of results is all ok. * Checks if the stream of results is all ok.
*/ */
export const everyPluginOk: OperatorFunction<VoidResult, boolean> = pipe( export const everyPluginOk: OperatorFunction<VoidResult, boolean> = pipe(
every(result => result.ok), every(result => result.isOk()),
defaultIfEmpty(true), defaultIfEmpty(true),
); );
@@ -74,10 +74,10 @@ export function handleError<C>(crashHandler: ErrorHandling, logging?: Logging) {
export const filterTap = <K, R>(onErr: (e: R) => void): OperatorFunction<Result<K, R>, K> => export const filterTap = <K, R>(onErr: (e: R) => void): OperatorFunction<Result<K, R>, K> =>
pipe( pipe(
concatMap(result => { concatMap(result => {
if(result.ok) { if(result.isOk()) {
return of(result.val) return of(result.value)
} }
onErr(result.val); onErr(result.error);
return EMPTY return EMPTY
}) })

View File

@@ -31,15 +31,21 @@ export class Context extends CoreContext<Message, ChatInputCommandInteraction> {
} }
public get id(): Snowflake { public get id(): Snowflake {
return this.ctx.val.id; return safeUnwrap(this.ctx
.map(m => m.id)
.mapErr(i => i.id));
} }
public get channel() { public get channel() {
return this.ctx.val.channel; return safeUnwrap(this.ctx
.map(m => m.channel)
.mapErr(i => i.channel));
} }
public get channelId(): Snowflake { 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<Message, ChatInputCommandInteraction> {
* else, interaction.user * else, interaction.user
*/ */
public get user(): 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 { public get userId(): Snowflake {
@@ -55,29 +63,41 @@ export class Context extends CoreContext<Message, ChatInputCommandInteraction> {
} }
public get createdTimestamp(): number { public get createdTimestamp(): number {
return this.ctx.val.createdTimestamp; return safeUnwrap(this.ctx
.map(m => m.createdTimestamp)
.mapErr(i => i.createdTimestamp));
} }
public get guild() { public get guild() {
return this.ctx.val.guild; return safeUnwrap(this.ctx
.map(m => m.guild)
.mapErr(i => i.guild));
} }
public get guildId() { 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 * interactions can return APIGuildMember if the guild it is emitted from is not cached
*/ */
public get member() { public get member() {
return this.ctx.val.member; return safeUnwrap(this.ctx
.map(m => m.member)
.mapErr(i => i.member));
} }
public get client(): Client { 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 { 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) { public async reply(content: ReplyOptions) {
@@ -100,5 +120,5 @@ export class Context extends CoreContext<Message, ChatInputCommandInteraction> {
} }
function safeUnwrap<T>(res: Result<T, T>) { function safeUnwrap<T>(res: Result<T, T>) {
return res.val; return res.unwrap()
} }

View File

@@ -7,7 +7,7 @@ import * as assert from 'node:assert';
*/ */
export abstract class CoreContext<M, I> { export abstract class CoreContext<M, I> {
protected constructor(protected ctx: Either<M, I>) { protected constructor(protected ctx: Either<M, I>) {
assert.ok(typeof ctx.val === 'object' && ctx.val != null); assert.ok(typeof ctx === 'object' && ctx != null);
} }
get message(): M { get message(): M {
return this.ctx.expect(SernError.MismatchEvent); return this.ctx.expect(SernError.MismatchEvent);
@@ -17,7 +17,7 @@ export abstract class CoreContext<M, I> {
} }
public isMessage(): this is CoreContext<M, never> { public isMessage(): this is CoreContext<M, never> {
return this.ctx.ok; return this.ctx.isOk();
} }
public isSlash(): this is CoreContext<never, I> { public isSlash(): this is CoreContext<never, I> {

View File

@@ -150,11 +150,11 @@ export function executeModule(
//converting the task into a promise so rxjs can resolve the Awaitable properly //converting the task into a promise so rxjs can resolve the Awaitable properly
concatMap(() => Result.wrapAsync(async () => task())), concatMap(() => Result.wrapAsync(async () => task())),
concatMap(result => { concatMap(result => {
if (result.ok) { if (result.isOk()) {
emitter.emit('module.activate', SernEmitter.success(module)); emitter.emit('module.activate', SernEmitter.success(module));
return EMPTY; return EMPTY;
} else { } 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); const task$ = config.createStream(args);
return task$.pipe( return task$.pipe(
tap(result => { tap(result => {
result.err && config.onStop?.(args.module); result.isErr() && config.onStop?.(args.module);
}), }),
everyPluginOk, everyPluginOk,
filterMapTo(() => config.onNext(args)), filterMapTo(() => config.onNext(args)),

View File

@@ -9,7 +9,6 @@ describe('module-loading', () => {
expect(filename).toBe(name) expect(filename).toBe(name)
}) })
// todo: handle commands with multiple extensions // todo: handle commands with multiple extensions
// it('should properly extract filename from file, nested multiple', () => { // it('should properly extract filename from file, nested multiple', () => {
// const extension = faker.system.fileExt() // const extension = faker.system.fileExt()

View File

@@ -18,7 +18,7 @@ function createRandomCommandModules() {
CommandType.Button, CommandType.Button,
]; ];
return commandModule({ return commandModule({
type: randomCommandType[Math.floor(Math.random() * randomCommandType.length)], type: faker.helpers.uniqueArray(randomCommandType, 1)[0],
description: faker.string.alpha(), description: faker.string.alpha(),
name: faker.string.alpha(), name: faker.string.alpha(),
execute: () => {}, execute: () => {},

View File

@@ -619,7 +619,7 @@ __metadata:
iti: ^0.6.0 iti: ^0.6.0
prettier: 2.8.8 prettier: 2.8.8
rxjs: ^7.8.0 rxjs: ^7.8.0
ts-results-es: latest ts-results-es: ^4.0.0
tsup: ^6.7.0 tsup: ^6.7.0
typescript: 5.0.2 typescript: 5.0.2
vitest: latest vitest: latest
@@ -3844,10 +3844,10 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"ts-results-es@npm:latest": "ts-results-es@npm:^4.0.0":
version: 3.6.1 version: 4.0.0
resolution: "ts-results-es@npm:3.6.1" resolution: "ts-results-es@npm:4.0.0"
checksum: af0d93ee4d3bd9e99a5fd4ac4b0ad090aef0a61e1f38ee596cfebe8d47090b34a2557d3778e00b4aae7c74962133805275ffffe56716e4d747fa559a926d9ced checksum: 32a7059491e36d06c5a1084fe9be8021a0beb2d94a94b0c3fa85dc3e96561bf34fb8fd60ebe661064c9fc2bafcf437b6b65f119e8d7497af7f76cda9d9a2a945
languageName: node languageName: node
linkType: hard linkType: hard