@mitralab.io/sdk-core 0.2.14 → 0.2.16-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -48,6 +48,9 @@ function isObject(value) {
48
48
  function isNullableString(value) {
49
49
  return value === null || typeof value === "string";
50
50
  }
51
+ function isNonEmptyString(value) {
52
+ return typeof value === "string" && value.length > 0;
53
+ }
51
54
  function isNullableBoolean(value) {
52
55
  return value === null || typeof value === "boolean";
53
56
  }
@@ -681,6 +684,83 @@ function expectMessageAccepted(value, context, errors = defaultSdkCoreErrorFacto
681
684
  if (typeof response.messageId !== "string") invalidField(context, "messageId", errors);
682
685
  return response;
683
686
  }
687
+ function expectEmailTemplate(value, context, errors = defaultSdkCoreErrorFactory) {
688
+ const template = expectObject(value, context, errors);
689
+ if (typeof template.id !== "string") invalidField(context, "id", errors);
690
+ if (!isNonEmptyString(template.type)) invalidField(context, "type", errors);
691
+ if (!isNullableString(template.senderIdentityId)) {
692
+ invalidField(context, "senderIdentityId", errors);
693
+ }
694
+ if (typeof template.subject !== "string") invalidField(context, "subject", errors);
695
+ if (typeof template.htmlBody !== "string") invalidField(context, "htmlBody", errors);
696
+ if (!isNullableString(template.textBody)) invalidField(context, "textBody", errors);
697
+ if (typeof template.platformDefault !== "boolean") {
698
+ invalidField(context, "platformDefault", errors);
699
+ }
700
+ if (typeof template.createdAt !== "string") invalidField(context, "createdAt", errors);
701
+ if (typeof template.updatedAt !== "string") invalidField(context, "updatedAt", errors);
702
+ return template;
703
+ }
704
+ function expectEmailTemplateTypeCatalog(value, context, errors = defaultSdkCoreErrorFactory) {
705
+ const catalog = expectObject(value, context, errors);
706
+ if (!isNonEmptyString(catalog.type)) invalidField(context, "type", errors);
707
+ expectObjectArray(catalog.variables, `${context} variables`, errors).forEach(
708
+ (variable, position) => {
709
+ const variableContext = `${context} variable ${position}`;
710
+ if (typeof variable.name !== "string") invalidField(variableContext, "name", errors);
711
+ if (typeof variable.required !== "boolean") {
712
+ invalidField(variableContext, "required", errors);
713
+ }
714
+ if (!isNullableString(variable.description)) {
715
+ invalidField(variableContext, "description", errors);
716
+ }
717
+ if (!isNullableString(variable.preview)) invalidField(variableContext, "preview", errors);
718
+ }
719
+ );
720
+ return catalog;
721
+ }
722
+ function expectEmailTemplatePreview(value, context, errors = defaultSdkCoreErrorFactory) {
723
+ const preview = expectObject(value, context, errors);
724
+ if (typeof preview.subject !== "string") invalidField(context, "subject", errors);
725
+ if (typeof preview.htmlBody !== "string") invalidField(context, "htmlBody", errors);
726
+ if (!isNullableString(preview.textBody)) invalidField(context, "textBody", errors);
727
+ return preview;
728
+ }
729
+ function expectEmailSender(value, context, errors = defaultSdkCoreErrorFactory) {
730
+ const sender = expectObject(value, context, errors);
731
+ if (typeof sender.id !== "string") invalidField(context, "id", errors);
732
+ if (typeof sender.email !== "string") invalidField(context, "email", errors);
733
+ if (typeof sender.displayName !== "string") invalidField(context, "displayName", errors);
734
+ if (!isNonEmptyString(sender.status)) invalidField(context, "status", errors);
735
+ if (typeof sender.sharedWithCurrentWorkspace !== "boolean") {
736
+ invalidField(context, "sharedWithCurrentWorkspace", errors);
737
+ }
738
+ if (typeof sender.ownerConfirmationPending !== "boolean") {
739
+ invalidField(context, "ownerConfirmationPending", errors);
740
+ }
741
+ if (typeof sender.createdAt !== "string") invalidField(context, "createdAt", errors);
742
+ if (typeof sender.updatedAt !== "string") invalidField(context, "updatedAt", errors);
743
+ return sender;
744
+ }
745
+ function expectEmailSettings(value, context, errors = defaultSdkCoreErrorFactory) {
746
+ const settings = expectObject(value, context, errors);
747
+ if (typeof settings.appId !== "string") invalidField(context, "appId", errors);
748
+ if (!isNullableString(settings.senderIdentityId)) {
749
+ invalidField(context, "senderIdentityId", errors);
750
+ }
751
+ if (!isNullableString(settings.displayName)) invalidField(context, "displayName", errors);
752
+ if (!isNullableString(settings.replyTo)) invalidField(context, "replyTo", errors);
753
+ if (typeof settings.ownTemplateSendsFromBrand !== "boolean") {
754
+ invalidField(context, "ownTemplateSendsFromBrand", errors);
755
+ }
756
+ const senderContext = `${context} effectiveSender`;
757
+ const sender = expectObject(settings.effectiveSender, senderContext, errors);
758
+ if (typeof sender.email !== "string") invalidField(senderContext, "email", errors);
759
+ if (!isNullableString(sender.displayName)) invalidField(senderContext, "displayName", errors);
760
+ if (!isNonEmptyString(sender.source)) invalidField(senderContext, "source", errors);
761
+ if (!isNullableString(sender.reason)) invalidField(senderContext, "reason", errors);
762
+ return settings;
763
+ }
684
764
  function expectAgentDefinition(value, context, errors = defaultSdkCoreErrorFactory) {
685
765
  const agent = expectObject(value, context, errors);
686
766
  if (typeof agent.id !== "string") invalidField(context, "id", errors);
@@ -2852,7 +2932,23 @@ function createMembersModule(transport, errors = defaultSdkCoreErrorFactory) {
2852
2932
  }
2853
2933
 
2854
2934
  // src/modules/messenger.ts
2855
- function createMessengerModule(transport, errors = defaultSdkCoreErrorFactory) {
2935
+ function createMessengerModule(transport, errors = defaultSdkCoreErrorFactory, getAppId) {
2936
+ const templatePath = (type) => `/api/v1/email/templates/${encodePathSegment(type, "email template type", errors)}`;
2937
+ const senderPath = (senderId) => `/api/v1/email/identities/${encodePathSegment(senderId, "email sender id", errors)}`;
2938
+ const currentAppId = () => {
2939
+ const appId = getAppId?.();
2940
+ if (!appId) configurationError("An appId is required for app email calls", errors);
2941
+ return appId;
2942
+ };
2943
+ const settingsPath = () => `/api/v1/email/apps/${encodePathSegment(currentAppId(), "app id", errors)}/settings`;
2944
+ const preview = async (type, appId) => expectEmailTemplatePreview(
2945
+ await transport.request(`${templatePath(type)}/preview`, {
2946
+ method: "GET",
2947
+ params: { appId }
2948
+ }),
2949
+ "Email template preview response",
2950
+ errors
2951
+ );
2856
2952
  return {
2857
2953
  async notify(content) {
2858
2954
  return expectMessageAccepted(
@@ -2873,6 +2969,120 @@ function createMessengerModule(transport, errors = defaultSdkCoreErrorFactory) {
2873
2969
  "Send email response",
2874
2970
  errors
2875
2971
  );
2972
+ },
2973
+ async listEmailTemplates() {
2974
+ return expectObjectArray(
2975
+ await transport.request("/api/v1/email/templates", {
2976
+ method: "GET",
2977
+ params: { appId: currentAppId() }
2978
+ }),
2979
+ "Email template list response",
2980
+ errors,
2981
+ expectEmailTemplate
2982
+ );
2983
+ },
2984
+ async getEmailTemplateType(type) {
2985
+ return expectEmailTemplateTypeCatalog(
2986
+ await transport.request(
2987
+ `/api/v1/email/template-types/${encodePathSegment(type, "email template type", errors)}`,
2988
+ { method: "GET" }
2989
+ ),
2990
+ "Email template type response",
2991
+ errors
2992
+ );
2993
+ },
2994
+ async getEmailTemplate(type) {
2995
+ return expectEmailTemplate(
2996
+ await transport.request(templatePath(type), {
2997
+ method: "GET",
2998
+ params: { appId: currentAppId() }
2999
+ }),
3000
+ "Email template response",
3001
+ errors
3002
+ );
3003
+ },
3004
+ async upsertEmailTemplate(type, input) {
3005
+ const appId = currentAppId();
3006
+ const template = expectEmailTemplate(
3007
+ await transport.request(templatePath(type), {
3008
+ method: "PUT",
3009
+ body: templateBody(appId, input)
3010
+ }),
3011
+ "Upsert email template response",
3012
+ errors
3013
+ );
3014
+ try {
3015
+ return { template, preview: await preview(type, appId) };
3016
+ } catch (previewError) {
3017
+ return { template, preview: null, previewError };
3018
+ }
3019
+ },
3020
+ async previewEmailTemplate(type) {
3021
+ return preview(type, currentAppId());
3022
+ },
3023
+ async deleteEmailTemplate(type) {
3024
+ expectEmpty(
3025
+ await transport.request(templatePath(type), {
3026
+ method: "DELETE",
3027
+ params: { appId: currentAppId() }
3028
+ }),
3029
+ "Delete email template response",
3030
+ errors
3031
+ );
3032
+ },
3033
+ async listEmailSenders() {
3034
+ return expectObjectArray(
3035
+ await transport.request("/api/v1/email/identities/workspace", { method: "GET" }),
3036
+ "Email sender list response",
3037
+ errors,
3038
+ expectEmailSender
3039
+ );
3040
+ },
3041
+ async registerEmailSender(input) {
3042
+ return expectEmailSender(
3043
+ await transport.request("/api/v1/email/identities", {
3044
+ method: "POST",
3045
+ body: { email: input.email, displayName: input.displayName }
3046
+ }),
3047
+ "Register email sender response",
3048
+ errors
3049
+ );
3050
+ },
3051
+ async resendEmailSenderVerification(senderId) {
3052
+ return expectEmailSender(
3053
+ await transport.request(`${senderPath(senderId)}/resend-verification`, {
3054
+ method: "POST"
3055
+ }),
3056
+ "Resend email sender verification response",
3057
+ errors
3058
+ );
3059
+ },
3060
+ async confirmEmailSender(senderId, code) {
3061
+ return expectEmailSender(
3062
+ await transport.request(`${senderPath(senderId)}/confirm`, {
3063
+ method: "POST",
3064
+ body: { code }
3065
+ }),
3066
+ "Confirm email sender response",
3067
+ errors
3068
+ );
3069
+ },
3070
+ async getEmailSettings() {
3071
+ return expectEmailSettings(
3072
+ await transport.request(settingsPath(), { method: "GET" }),
3073
+ "Email settings response",
3074
+ errors
3075
+ );
3076
+ },
3077
+ async updateEmailSettings(input) {
3078
+ return expectEmailSettings(
3079
+ await transport.request(settingsPath(), {
3080
+ method: "PUT",
3081
+ body: settingsBody(input)
3082
+ }),
3083
+ "Update email settings response",
3084
+ errors
3085
+ );
2876
3086
  }
2877
3087
  };
2878
3088
  }
@@ -2888,6 +3098,21 @@ function rawEmailPayload(input) {
2888
3098
  ...input.text === void 0 ? {} : { text: input.text }
2889
3099
  };
2890
3100
  }
3101
+ function templateBody(appId, input) {
3102
+ return {
3103
+ appId,
3104
+ subject: input.subject,
3105
+ htmlBody: input.htmlBody,
3106
+ ...input.textBody === void 0 ? {} : { textBody: input.textBody }
3107
+ };
3108
+ }
3109
+ function settingsBody(input) {
3110
+ return {
3111
+ ...input.senderIdentityId === void 0 ? {} : { senderIdentityId: input.senderIdentityId },
3112
+ ...input.displayName === void 0 ? {} : { displayName: input.displayName },
3113
+ ...input.replyTo === void 0 ? {} : { replyTo: input.replyTo }
3114
+ };
3115
+ }
2891
3116
 
2892
3117
  // src/modules/publicFunctions.ts
2893
3118
  function createPublicFunctionsModule(transport, errors = defaultSdkCoreErrorFactory) {
@@ -3205,7 +3430,7 @@ function createSdkCore(options) {
3205
3430
  integrationResources: createIntegrationResourcesModule(options.transports.integration, errors),
3206
3431
  integrationTemplates: createIntegrationTemplatesModule(options.transports.integration, errors),
3207
3432
  members,
3208
- messenger: createMessengerModule(messengerTransport, errors),
3433
+ messenger: createMessengerModule(messengerTransport, errors, options.getAppId),
3209
3434
  publicFunctions: createPublicFunctionsModule(options.transports.publicFunctions, errors),
3210
3435
  queries: createQueriesModule(options.transports.dataManager, errors),
3211
3436
  sql: createSqlModule(options.transports.dataManager, errors),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mitralab.io/sdk-core",
3
- "version": "0.2.14",
3
+ "version": "0.2.16-beta.0",
4
4
  "description": "Environment-neutral contracts and modules shared by Mitra JavaScript SDKs",
5
5
  "type": "module",
6
6
  "sideEffects": false,