saasco-sdk 0.1.20 → 0.1.21

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/index.esm.js CHANGED
@@ -2,7 +2,7 @@ import { __awaiter } from 'tslib';
2
2
  import { v4 } from '@lukeed/uuid';
3
3
  import { z } from 'zod';
4
4
 
5
- var version = "0.1.20";
5
+ var version = "0.1.21";
6
6
 
7
7
  const timezones = {
8
8
  'Asia/Barnaul': 'RU',
@@ -811,107 +811,6 @@ class Saasco {
811
811
  }
812
812
  }
813
813
 
814
- function coerceReservedProperties(properties) {
815
- const coerce = [{
816
- key: 'age',
817
- coerceFrom: ['age', 'age_value', 'ageValue', 'user_age', 'userAge', 'user_age_value', 'userAgeValue', 'age_of_user', 'ageOfUser', 'age_in_years', 'ageInYears', 'years_old', 'yearsOld', 'age_years', 'ageYears'],
818
- schema: z.coerce.number().optional()
819
- }, {
820
- key: 'avatar',
821
- coerceFrom: ['avatar', 'user_avatar', 'userAvatar', 'avatar_url', 'avatarUrl', 'profile_picture', 'profilePicture', 'picture', 'user_picture', 'userPicture', 'profile_pic', 'profilePic', 'pic', 'user_pic', 'userPic', 'image', 'user_image', 'userImage', 'profile_image', 'profileImage', 'profile_image_url', 'profileImageUrl', 'profile_pic_url', 'profilePicUrl', 'pic_url', 'picUrl', 'image_url', 'imageUrl', 'user_image_url', 'userImageUrl', 'user_pic_url', 'userPicUrl', 'profile_pic_url', 'profilePicUrl', 'pic_url', 'picUrl', 'image_url', 'imageUrl', 'user_image_url', 'userImageUrl', 'user_pic_url', 'userPicUrl'],
822
- schema: z.coerce.string().url().optional()
823
- }, {
824
- key: 'birthday',
825
- coerceFrom: ['birthday', 'birth_date', 'birthDate', 'date_of_birth', 'dateOfBirth', 'dob', 'Dob', 'birth_day', 'birthDay']
826
- }, {
827
- key: 'createdAt',
828
- coerceFrom: ['createdAt', 'created_at', 'created', 'createdDate', 'created_date', 'dateCreated', 'date_created', 'date_created_at', 'dateCreatedAt', 'date_created_at', 'dateCreated', 'date_created'],
829
- schema: z.coerce.date().optional()
830
- }, {
831
- key: 'description',
832
- coerceFrom: ['description', 'bio', 'about', 'about_me', 'aboutMe', 'user_description', 'userDescription', 'profile_description', 'profileDescription']
833
- }, {
834
- key: 'email',
835
- coerceFrom: ['email', 'user_email', 'userEmail', 'email_address', 'emailAddress', 'e_mail', 'E_Mail', 'mail', 'Mail', 'contact_email', 'contactEmail', 'primary_email', 'primaryEmail'],
836
- schema: z.coerce.string().email().optional()
837
- }, {
838
- key: 'firstName',
839
- coerceFrom: ['first_name', 'firstName', 'firstname', 'given_name', 'givenName', 'user_first_name', 'userFirstName', 'name_first', 'nameFirst', 'f_name', 'fName']
840
- }, {
841
- key: 'gender',
842
- coerceFrom: ['gender', 'user_gender', 'userGender', 'sex', 'user_sex', 'userSex']
843
- }, {
844
- key: 'lastName',
845
- coerceFrom: ['last_name', 'lastName', 'lastname', 'surname', 'user_last_name', 'userLastName', 'name_last', 'nameLast', 'l_name', 'lName']
846
- }, {
847
- key: 'name',
848
- coerceFrom: ['name', 'full_name', 'fullName', 'user_name', 'userName', 'complete_name', 'completeName']
849
- }, {
850
- key: 'phone',
851
- coerceFrom: ['phone', 'phone_number', 'phoneNumber', 'mobile', 'mobile_number', 'mobileNumber', 'contact_number', 'contactNumber', 'tel', 'telephone']
852
- }, {
853
- key: 'title',
854
- coerceFrom: ['title', 'user_title', 'userTitle', 'position', 'job_title', 'jobTitle']
855
- }, {
856
- key: 'username',
857
- coerceFrom: ['username', 'user_name', 'userName', 'user_login', 'userLogin', 'account_name', 'accountName', 'screen_name', 'screenName', 'handle']
858
- }, {
859
- key: 'website',
860
- coerceFrom: ['website', 'web_site', 'site', 'web', 'personal_website', 'personalWebsite', 'company_website', 'companyWebsite'],
861
- schema: z.coerce.string().url().optional()
862
- }, {
863
- key: 'displayName',
864
- coerceFrom: ['display_name', 'displayName', 'user_display_name', 'userDisplayName', 'full_name', 'fullName', 'user_full_name', 'userFullName', 'complete_name', 'completeName', 'user_complete_name', 'userCompleteName', 'name', 'username']
865
- }, {
866
- key: '$stripeCustomerId',
867
- coerceFrom: ['stripe_customer_id', 'stripeCustomerId', 'stripe_customer_id', 'stripeCustomerId', 'stripe_customer_id', 'stripeCustomerId', 'stripe_customer_id', 'stripeCustomerId', 'stripe_customer_id', 'stripeCustomerId']
868
- }];
869
- return coerce.reduce((acc, {
870
- key,
871
- coerceFrom,
872
- schema
873
- }) => {
874
- const coercedValue = coerceValue(properties, coerceFrom, schema);
875
- if (coercedValue) acc[key] = coercedValue;
876
- if (!coercedValue && key === 'displayName') {
877
- let displayName = '';
878
- if (acc['firstName']) {
879
- displayName = `${acc['firstName'] || ''} ${acc['lastName'] || ''}`.trim();
880
- } else if (acc['username'] && typeof acc['username'] === 'string') {
881
- displayName = acc['username'];
882
- } else if (acc['name'] && typeof acc['name'] === 'string') {
883
- displayName = acc['name'];
884
- }
885
- if (displayName) acc[key] = displayName;
886
- }
887
- return acc;
888
- }, properties);
889
- }
890
- /**
891
- * Coerces the first non-null value from the properties object
892
- * @param properties
893
- * @param keys Provided in priority order, the first to match will return
894
- * @returns
895
- */
896
- function coerceValue(properties, keys, schema) {
897
- let coercedValue = null;
898
- for (const key of keys) {
899
- if (properties[key] !== undefined) {
900
- if (!schema) {
901
- coercedValue = properties[key];
902
- break;
903
- } else {
904
- const result = schema.safeParse(properties[key]);
905
- if (result.success) {
906
- coercedValue = properties[key];
907
- break;
908
- }
909
- }
910
- }
911
- }
912
- return coercedValue;
913
- }
914
-
915
814
  const browserContextSchema = z.object({
916
815
  $locale: z.string(),
917
816
  $location: z.string(),
@@ -924,284 +823,5 @@ const browserContextSchema = z.object({
924
823
  $title: z.string(),
925
824
  $userAgent: z.string()
926
825
  });
927
- const serverContextSchema = z.object({
928
- $city: z.string().optional(),
929
- $country: z.string().optional(),
930
- $continent: z.string().optional(),
931
- $latitude: z.number().optional(),
932
- $longitude: z.number().optional(),
933
- $timezone: z.string().optional(),
934
- $userAgent: z.string().optional(),
935
- $referrer: z.string().optional(),
936
- $ip: z.string().optional(),
937
- $processedAt: z.string().datetime().optional(),
938
- $identityHash: z.string().optional()
939
- });
940
- const trackedEventTypesSchema = z.enum(['SHORTLINK_REDIRECT']);
941
- const trackedEventSchema = z.object({
942
- id: z.string().uuid(),
943
- timestamp: z.string().datetime(),
944
- projectId: z.string(),
945
- version: z.string(),
946
- type: trackedEventTypesSchema,
947
- payload: z.object({
948
- browserContext: browserContextSchema.optional(),
949
- serverContext: serverContextSchema.optional(),
950
- properties: z.record(z.string(), z.any()).optional()
951
- })
952
- });
953
- /**
954
- * Creates a zod for tracking any type of event.
955
- * Includes the base tracking schema and extends with any new schema
956
- * @param type
957
- * @param payloadSchema
958
- * @returns
959
- */
960
- function createTrackedEventTypeSchema(type, payloadSchema) {
961
- const typeSchema = z.literal(type);
962
- return trackedEventSchema.extend({
963
- type: typeSchema,
964
- payload: z.object(Object.assign(Object.assign({}, trackedEventSchema.shape.payload.shape), {
965
- properties: payloadSchema
966
- }))
967
- });
968
- }
969
-
970
- const tbAny = z.union([z.string(), z.number(), z.array(z.union([z.string(), z.number()]))]);
971
- const tinyBirdBaseSchema = z.object({
972
- meta: z.array(z.object({
973
- name: z.string(),
974
- type: z.string()
975
- })),
976
- statistics: z.object({
977
- elapsed: z.number(),
978
- rows_read: z.number(),
979
- bytes_read: z.number()
980
- }),
981
- rows: z.number()
982
- });
983
- const TinybirdJobResponseSchema = z.object({
984
- id: z.string(),
985
- job_id: z.string(),
986
- job_url: z.string().url(),
987
- job: z.object({
988
- kind: z.string(),
989
- id: z.string(),
990
- job_id: z.string(),
991
- status: z.string(),
992
- created_at: z.string(),
993
- updated_at: z.string(),
994
- started_at: z.string().nullable(),
995
- is_cancellable: z.boolean(),
996
- datasource: z.object({
997
- id: z.string(),
998
- name: z.string()
999
- }),
1000
- delete_condition: z.string().optional()
1001
- }),
1002
- status: z.string(),
1003
- delete_id: z.string().optional(),
1004
- import_id: z.string().optional()
1005
- });
1006
- const latestEventsRequestSchema = z.object({
1007
- projectId: z.string(),
1008
- limit: z.number().optional(),
1009
- payload: z.boolean().optional(),
1010
- distinctId: z.string().optional(),
1011
- action: z.string().optional()
1012
- });
1013
- const latestEventItemSchema = z.object({
1014
- id: z.string(),
1015
- timestamp: z.string(),
1016
- action: z.string(),
1017
- location: z.string(),
1018
- referrer: z.string(),
1019
- href: z.string(),
1020
- device: z.string(),
1021
- browser: z.string(),
1022
- payload: z.string().optional(),
1023
- distinctId: z.string().optional(),
1024
- unique: z.boolean().optional()
1025
- });
1026
- const latestEventsResponseSchema = tinyBirdBaseSchema.extend({
1027
- data: z.array(latestEventItemSchema)
1028
- });
1029
- const kpisRequestSchema = z.object({
1030
- projectId: z.string(),
1031
- dateTo: z.string().optional(),
1032
- dateFrom: z.string().optional(),
1033
- referrer: z.string().optional(),
1034
- location: z.string().optional()
1035
- });
1036
- const kpiSchema = z.object({
1037
- date: z.string(),
1038
- users: z.number(),
1039
- sessions: z.number(),
1040
- pageViews: z.number(),
1041
- events: z.number(),
1042
- bounceRate: z.nullable(z.number()),
1043
- avgSessionSec: z.number()
1044
- });
1045
- const kpisResponseSchema = tinyBirdBaseSchema.extend({
1046
- data: z.array(kpiSchema)
1047
- });
1048
- const baseTopRequestSchema = z.object({
1049
- projectId: z.string(),
1050
- dateTo: z.string().optional(),
1051
- dateFrom: z.string().optional(),
1052
- limit: z.number().optional(),
1053
- skip: z.number().optional(),
1054
- referrer: z.string().optional(),
1055
- location: z.string().optional()
1056
- });
1057
- const topPagesResponseSchema = tinyBirdBaseSchema.extend({
1058
- data: z.array(z.object({
1059
- pathname: z.string(),
1060
- users: z.number(),
1061
- events: z.number()
1062
- }))
1063
- });
1064
- const topLocationsResponseSchema = tinyBirdBaseSchema.extend({
1065
- data: z.array(z.object({
1066
- location: z.string(),
1067
- users: z.number(),
1068
- sessions: z.number(),
1069
- pageViews: z.number(),
1070
- bounceRate: z.nullable(z.number()),
1071
- avgSessionSec: z.number()
1072
- }))
1073
- });
1074
- const topDevicesResponseSchema = tinyBirdBaseSchema.extend({
1075
- data: z.array(z.object({
1076
- device: z.string(),
1077
- visits: z.number(),
1078
- hits: z.number(),
1079
- bounceRate: z.nullable(z.number()),
1080
- avgSessionSec: z.number()
1081
- }))
1082
- });
1083
- const topBrowsersResponseSchema = tinyBirdBaseSchema.extend({
1084
- data: z.array(z.object({
1085
- browser: z.string(),
1086
- visits: z.number(),
1087
- hits: z.number(),
1088
- bounceRate: z.nullable(z.number()),
1089
- avgSessionSec: z.number()
1090
- }))
1091
- });
1092
- const topSourcesSchema = z.object({
1093
- referrer: z.string(),
1094
- users: z.number(),
1095
- sessions: z.number(),
1096
- pageViews: z.number(),
1097
- bounceRate: z.nullable(z.number()),
1098
- avgSessionSec: z.number()
1099
- });
1100
- const topSourcesResponseSchema = tinyBirdBaseSchema.extend({
1101
- data: z.array(topSourcesSchema)
1102
- });
1103
- const topActionsResponseSchema = tinyBirdBaseSchema.extend({
1104
- data: z.array(z.object({
1105
- action: z.string(),
1106
- users: z.number(),
1107
- events: z.number()
1108
- }))
1109
- });
1110
- const analyticsEventSchema = z.object({
1111
- id: z.string(),
1112
- timestamp: z.string(),
1113
- projectId: z.string(),
1114
- anonymousId: z.string(),
1115
- sessionId: z.string(),
1116
- distinctId: z.string().nullable(),
1117
- version: z.string(),
1118
- action: z.string(),
1119
- payload: z.string()
1120
- });
1121
- const userEventsResponseSchema = tinyBirdBaseSchema.extend({
1122
- data: z.array(analyticsEventSchema)
1123
- });
1124
- const currentUsersRequestSchema = z.object({
1125
- projectId: z.string(),
1126
- minutes: z.number(),
1127
- referrer: z.string().optional(),
1128
- location: z.string().optional()
1129
- });
1130
- const currentUsersResponseSchema = tinyBirdBaseSchema.extend({
1131
- data: z.array(z.object({
1132
- visits: z.number()
1133
- }))
1134
- });
1135
- const reservedPropertiesSchema = z.object({
1136
- age: z.number().nullish(),
1137
- avatar: z.string().url().nullish(),
1138
- birthday: z.string().nullish(),
1139
- createdAt: z.string().nullish(),
1140
- description: z.string().nullish(),
1141
- email: z.string().email().or(z.literal('')).nullish(),
1142
- firstName: z.string().nullish(),
1143
- gender: z.string().nullish(),
1144
- id: z.string().nullish(),
1145
- lastName: z.string().nullish(),
1146
- name: z.string().nullish(),
1147
- phone: z.string().nullish(),
1148
- title: z.string().nullish(),
1149
- username: z.string().nullish(),
1150
- website: z.string().nullish(),
1151
- // Default Properties
1152
- $id: z.string().nullish(),
1153
- $lastSeen: z.string().nullish(),
1154
- $lastIdentifiedAt: z.string().nullish(),
1155
- $unsubscribed: z.boolean().nullish(),
1156
- $unsubscribeReason: z.enum(['manual', 'complained', 'bounced']).nullish(),
1157
- // Stripe Properties
1158
- $stripeCustomerId: z.string().nullish(),
1159
- $stripeTotalPayments: z.number().or(z.string()).nullish(),
1160
- $stripeTotalSpent: z.number().or(z.string()).nullish(),
1161
- // Internal properties
1162
- $_toDelete: z.boolean().nullish()
1163
- });
1164
- const contactPropertiesSchema = reservedPropertiesSchema.optional().and(z.record(z.union([z.string(), z.number(), z.boolean(), z.date(), z.null(), z.array(z.union([z.string(), z.number(), z.boolean(), z.date(), z.null()]))])));
1165
- const analyticsUserRawSchema = z.object({
1166
- timestamp: z.string(),
1167
- distinctId: z.string(),
1168
- projectId: z.string(),
1169
- payload: z.string()
1170
- });
1171
- const analyticsUserSchema = analyticsUserRawSchema.omit({
1172
- payload: true
1173
- }).and(z.object({
1174
- properties: contactPropertiesSchema
1175
- }));
1176
- const listUsersParamsSchema = z.object({
1177
- projectId: z.string(),
1178
- limit: z.number().optional()
1179
- });
1180
- const getAnalyticsUserParamsSchema = z.object({
1181
- projectId: z.string(),
1182
- distinctId: z.string()
1183
- });
1184
- const idenfitySchema = z.object({
1185
- id: z.string(),
1186
- timestamp: z.string(),
1187
- projectId: z.string(),
1188
- distinctId: z.string(),
1189
- anonymousId: z.string(),
1190
- version: z.string(),
1191
- payload: contactPropertiesSchema,
1192
- context: z.object({
1193
- active: z.boolean().optional()
1194
- }).optional()
1195
- });
1196
- const latestIdentifiesRequestSchema = z.object({
1197
- projectId: z.string(),
1198
- limit: z.number().optional(),
1199
- fields: z.array(z.string()).optional()
1200
- });
1201
- const latestIdentifiesResponseSchema = tinyBirdBaseSchema.extend({
1202
- data: z.array(z.object({
1203
- properties: contactPropertiesSchema
1204
- }))
1205
- });
1206
826
 
1207
- export { Saasco, TinybirdJobResponseSchema, analyticsEventSchema, analyticsUserRawSchema, analyticsUserSchema, baseTopRequestSchema, browserContextSchema, coerceReservedProperties, contactPropertiesSchema, createTrackedEventTypeSchema, currentUsersRequestSchema, currentUsersResponseSchema, getAnalyticsUserParamsSchema, getBrowserContext, idenfitySchema, kpiSchema, kpisRequestSchema, kpisResponseSchema, latestEventItemSchema, latestEventsRequestSchema, latestEventsResponseSchema, latestIdentifiesRequestSchema, latestIdentifiesResponseSchema, listUsersParamsSchema, reservedPropertiesSchema, serverContextSchema, tbAny, timezones, tinyBirdBaseSchema, topActionsResponseSchema, topBrowsersResponseSchema, topDevicesResponseSchema, topLocationsResponseSchema, topPagesResponseSchema, topSourcesResponseSchema, topSourcesSchema, trackedEventSchema, trackedEventTypesSchema, userEventsResponseSchema };
827
+ export { Saasco, browserContextSchema, getBrowserContext, timezones };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "saasco-sdk",
3
- "version": "0.1.20",
3
+ "version": "0.1.21",
4
4
  "dependencies": {
5
5
  "tslib": "^2.3.0",
6
6
  "@lukeed/uuid": "^2.0.1",
package/src/index.d.ts CHANGED
@@ -1,6 +1,4 @@
1
1
  export * from './lib/analytics';
2
- export * from './lib/coerceReservedProperties';
3
2
  export * from './lib/getBrowserContext';
4
- export * from './lib/timezones';
5
3
  export * from './lib/tracking';
6
- export * from './lib/types';
4
+ export * from './lib/timezones';
@@ -1,2 +1,2 @@
1
- import { BrowserContext } from './tracking/types';
1
+ import { BrowserContext } from './tracking';
2
2
  export declare function getBrowserContext(): BrowserContext | undefined;