From f6f4547af364b5eff70ac83b9fa5e2bed28be2e7 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 19 Jan 2023 16:24:31 +0530 Subject: [PATCH] chore: introduced string to json from user-settings and moved types --- .../src/subscription-handler.ts | 17 +++++------------ .../src/types/subscription-types.ts | 7 +++++++ packages/hoppscotch-backend/src/utils.ts | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 packages/hoppscotch-backend/src/types/subscription-types.ts diff --git a/packages/hoppscotch-backend/src/subscription-handler.ts b/packages/hoppscotch-backend/src/subscription-handler.ts index fba5551a4..a47730c3b 100644 --- a/packages/hoppscotch-backend/src/subscription-handler.ts +++ b/packages/hoppscotch-backend/src/subscription-handler.ts @@ -1,18 +1,11 @@ import { Injectable } from '@nestjs/common'; import { PubSubService } from './pubsub/pubsub.service'; import { PrimitiveTypes } from './types/primitive-types'; -import { ModuleTypes } from './types/module-types'; +import { CustomModuleTypes } from './types/custom-module-types'; +import { SubscriptionType } from './types/subscription-types'; // Custom generic type to indicate the type of module -type ModuleType = PrimitiveTypes | ModuleTypes; - -// Contains constants for the subscription types we use in Subscription Handler -enum SubscriptionType { - Created = 'created', - Updated = 'updated', - Deleted = 'deleted', - DeleteMany = 'delete_many', -} +type ModuleType = PrimitiveTypes | CustomModuleTypes; @Injectable() export class SubscriptionHandler { @@ -20,8 +13,8 @@ export class SubscriptionHandler { /** * Publishes a subscription using the pubsub module - * @param topic a string containing the module name, an uid and the type of subscription - * @param subscriptionType type of subscription being called + * @param topic a string containing the "module_name/identifier" + * @param subscriptionType type of subscription being published * @param moduleType type of the module model being called * @returns a promise of type void */ diff --git a/packages/hoppscotch-backend/src/types/subscription-types.ts b/packages/hoppscotch-backend/src/types/subscription-types.ts new file mode 100644 index 000000000..ee7ab75f2 --- /dev/null +++ b/packages/hoppscotch-backend/src/types/subscription-types.ts @@ -0,0 +1,7 @@ +// Contains constants for the subscription types we use in Subscription Handler +export enum SubscriptionType { + Created = 'created', + Updated = 'updated', + Deleted = 'deleted', + DeleteMany = 'delete_many', +} diff --git a/packages/hoppscotch-backend/src/utils.ts b/packages/hoppscotch-backend/src/utils.ts index a4756f5e6..570313640 100644 --- a/packages/hoppscotch-backend/src/utils.ts +++ b/packages/hoppscotch-backend/src/utils.ts @@ -4,8 +4,10 @@ import { pipe } from 'fp-ts/lib/function'; import * as O from 'fp-ts/Option'; import * as TE from 'fp-ts/TaskEither'; import * as T from 'fp-ts/Task'; +import * as E from 'fp-ts/Either'; import { User } from './user/user.model'; import * as A from 'fp-ts/Array'; +import { JSON_INVALID } from './errors'; /** * A workaround to throw an exception in an expression. @@ -108,3 +110,18 @@ export const taskEitherValidateArraySeq = ( TE.getApplicativeTaskValidation(T.ApplicativeSeq, A.getMonoid()), ), ); + +/** + * String to JSON parser + * @param {str} str The string to parse + * @returns {E.Right | E.Left<"json_invalid">} An Either of the parsed JSON + */ +export function stringToJson( + str: string, +): E.Right | E.Left { + try { + return E.right(JSON.parse(str)); + } catch (err) { + return E.left(JSON_INVALID); + } +}