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

@@ -66,12 +66,7 @@ export async function composeRoot(
}
//Build the container based on the callback provided by the user
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) {
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.
*/
export const everyPluginOk: OperatorFunction<VoidResult, boolean> = pipe(
every(result => result.ok),
every(result => result.isOk()),
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> =>
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
})

View File

@@ -31,15 +31,21 @@ export class Context extends CoreContext<Message, ChatInputCommandInteraction> {
}
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<Message, ChatInputCommandInteraction> {
* 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<Message, ChatInputCommandInteraction> {
}
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<Message, ChatInputCommandInteraction> {
}
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> {
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 {
return this.ctx.expect(SernError.MismatchEvent);
@@ -17,7 +17,7 @@ export abstract class CoreContext<M, I> {
}
public isMessage(): this is CoreContext<M, never> {
return this.ctx.ok;
return this.ctx.isOk();
}
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
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)),