@tellescope/sdk 0.0.4 → 0.0.8

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.
Files changed (45) hide show
  1. package/lib/cjs/enduser.d.ts +346 -13
  2. package/lib/cjs/enduser.d.ts.map +1 -1
  3. package/lib/cjs/enduser.js +135 -14
  4. package/lib/cjs/enduser.js.map +1 -1
  5. package/lib/cjs/sdk.d.ts +106 -19
  6. package/lib/cjs/sdk.d.ts.map +1 -1
  7. package/lib/cjs/sdk.js +116 -32
  8. package/lib/cjs/sdk.js.map +1 -1
  9. package/lib/cjs/session.d.ts +36 -175
  10. package/lib/cjs/session.d.ts.map +1 -1
  11. package/lib/cjs/session.js +131 -34
  12. package/lib/cjs/session.js.map +1 -1
  13. package/lib/cjs/tests/public_endpoint_tests.js +3 -3
  14. package/lib/cjs/tests/public_endpoint_tests.js.map +1 -1
  15. package/lib/cjs/tests/socket_tests.js +42 -31
  16. package/lib/cjs/tests/socket_tests.js.map +1 -1
  17. package/lib/cjs/tests/tests.js +196 -62
  18. package/lib/cjs/tests/tests.js.map +1 -1
  19. package/lib/esm/enduser.d.ts +346 -13
  20. package/lib/esm/enduser.d.ts.map +1 -1
  21. package/lib/esm/enduser.js +132 -12
  22. package/lib/esm/enduser.js.map +1 -1
  23. package/lib/esm/sdk.d.ts +106 -19
  24. package/lib/esm/sdk.d.ts.map +1 -1
  25. package/lib/esm/sdk.js +116 -32
  26. package/lib/esm/sdk.js.map +1 -1
  27. package/lib/esm/session.d.ts +36 -175
  28. package/lib/esm/session.d.ts.map +1 -1
  29. package/lib/esm/session.js +130 -32
  30. package/lib/esm/session.js.map +1 -1
  31. package/lib/esm/tests/public_endpoint_tests.js +3 -3
  32. package/lib/esm/tests/public_endpoint_tests.js.map +1 -1
  33. package/lib/esm/tests/socket_tests.js +41 -30
  34. package/lib/esm/tests/socket_tests.js.map +1 -1
  35. package/lib/esm/tests/tests.js +192 -63
  36. package/lib/esm/tests/tests.js.map +1 -1
  37. package/lib/tsconfig.tsbuildinfo +1 -1
  38. package/package.json +11 -9
  39. package/src/enduser.ts +93 -20
  40. package/src/sdk.ts +70 -42
  41. package/src/session.ts +121 -33
  42. package/src/tests/public_endpoint_tests.ts +6 -4
  43. package/src/tests/socket_tests.ts +37 -16
  44. package/src/tests/tests.ts +117 -24
  45. package/tsconfig.json +0 -1
@@ -6,34 +6,44 @@ import {
6
6
  ClientModelForName,
7
7
  ClientModelForName_required,
8
8
  } from "@tellescope/types-client"
9
+ import {
10
+ Model as ClientModel,
11
+ ModelName,
12
+ } from "@tellescope/types-models"
9
13
 
10
-
11
- // import v from 'validator'
14
+ import {
15
+ Indexable,
16
+ Operation,
17
+ } from "@tellescope/types-utilities"
12
18
 
13
19
  import {
14
20
  fieldsToValidation,
15
21
  mongoIdValidator,
16
22
 
17
- // InputValidation,
23
+ InputValidation,
18
24
  } from "@tellescope/validation"
19
25
 
20
- import { Session, APIQuery } from "../sdk"
21
- import { url_safe_path } from "@tellescope/utilities"
26
+ import { Session, APIQuery, EnduserSession } from "../sdk"
27
+ import { } from "@tellescope/utilities"
22
28
  import { DEFAULT_OPERATIONS } from "@tellescope/constants"
23
29
  import {
24
30
  schema,
25
- // Model,
26
- // ModelFields
31
+ Model,
32
+ ModelFields,
27
33
  } from "@tellescope/schema"
28
34
 
29
35
  import {
30
36
  assert,
31
37
  async_test,
32
38
  log_header,
33
- objects_equivalent,
34
39
  wait,
35
40
  } from "@tellescope/testing"
36
41
 
42
+ import {
43
+ objects_equivalent,
44
+ url_safe_path,
45
+ } from "@tellescope/utilities"
46
+
37
47
 
38
48
  const host = process.env.TEST_URL || 'http://localhost:8080'
39
49
  const [email, password] = [process.env.TEST_EMAIL, process.env.TEST_PASSWORD]
@@ -41,8 +51,28 @@ const [email2, password2] = [process.env.TEST_EMAIL_2, process.env.TEST_PASSWORD
41
51
 
42
52
  const userId = '60398b0231a295e64f084fd9'
43
53
 
54
+ // const example_SDK_usage = async () => {
55
+ // // initialize SDK and authenticate a user
56
+ // const sdk = new Session()
57
+ // await sdk.authenticate("user-email", 'user-password')
58
+
59
+ // // create a record, using Enduser model as example
60
+ // const enduser = await sdk.api.endusers.createOne({ email: "enduser1@tellescope.com" })
61
+
62
+ // // update an existing record
63
+ // await sdk.api.endusers.updateOne(enduser.id, { phone: "+15555555555" })
64
+
65
+ // // fetch a record by id, or a list of recent records
66
+ // const existingEnduser: Enduser = await sdk.api.endusers.getOne(enduser.id)
67
+ // const existingEndusers: Enduser[] = await sdk.api.endusers.getSome({ limit: 5, lastId: enduser.id })
68
+
69
+ // // delete an individual record
70
+ // await sdk.api.endusers.deleteOne(enduser.id)
71
+ // }
72
+
44
73
  const sdk = new Session()
45
74
  const sdkOther = new Session({ host, apiKey: "ba745e25162bb95a795c5fa1af70df188d93c4d3aac9c48b34a5c8c9dd7b80f7" })
75
+ const enduserSDK = new EnduserSession({ host })
46
76
  // const sdkOtherEmail = "sebass@tellescope.com"
47
77
 
48
78
  if (!(email && password && email2 && password2)) {
@@ -299,7 +329,7 @@ const run_generated_tests = async <N extends ModelName>({ queries, model, name,
299
329
  )
300
330
  await async_test(
301
331
  `get-${safeName}`,
302
- () => queries.getSome({}, filter),
332
+ () => queries.getSome({ filter }),
303
333
  { onResult: ([d, /*...others */]) => {
304
334
  // if (others.length !== 0) return false // some collections are not reset during testing, like API keys
305
335
  if (!d?.id) return false
@@ -527,7 +557,7 @@ const journey_tests = async (queries=sdk.api.journeys) => {
527
557
 
528
558
  await async_test(
529
559
  `journey-updateState`,
530
- () => queries.updateState(journey.id, 'Added', { name: 'Updated', priority: 'N/A' }),
560
+ () => queries.updateState({ id: journey.id, name: 'Added', updates: { name: 'Updated', priority: 'N/A' }}),
531
561
  passOnVoid,
532
562
  )
533
563
  await wait(undefined, 25) // wait for side effects to update endusers
@@ -701,7 +731,7 @@ const chat_tests = async() => {
701
731
  )
702
732
  await async_test(
703
733
  `get-chats (with filter)`,
704
- () => sdk.api.chats.getSome({}, { roomId: room.id }),
734
+ () => sdk.api.chats.getSome({ filter: { roomId: room.id } }),
705
735
  { onResult: c => c?.length === 2 }
706
736
  )
707
737
 
@@ -712,25 +742,27 @@ const chat_tests = async() => {
712
742
  )
713
743
  await async_test(
714
744
  `get-chats not allowed`,
715
- () => sdk2.api.chats.getSome({}, { roomId: room.id }),
716
- { shouldError: true, onError: e => e.message === 'You do not have permission to access this resource' }
717
- )
718
- await async_test(
719
- `update-chat not allowed`,
720
- () => sdk2.api.chats.updateOne(chat.id, { message: 'Hi' }),
745
+ () => sdk2.api.chats.getSome({ filter: { roomId: room.id } }),
721
746
  { shouldError: true, onError: e => e.message === 'You do not have permission to access this resource' }
722
747
  )
748
+ // currently disabled endpoint altogether
749
+ // await async_test(
750
+ // `update-chat not allowed`,
751
+ // () => sdk2.api.chats.updateOne(chat.id, { message: 'Hi' }),
752
+ // { shouldError: true, onError: e => e.message === 'You do not have permission to access this resource' }
753
+ // )
723
754
  await async_test(
724
755
  `delete-chat not allowed`,
725
756
  () => sdk2.api.chats.deleteOne(chat.id),
726
757
  { shouldError: true, onError: e => e.message === 'You do not have permission to access this resource' }
727
758
  )
728
759
 
729
- await async_test(
730
- `update-chat can't update roomId`,
731
- () => sdk.api.chats.updateOne(chat.id, { roomId: room.id } as any), // cast to any to allow calling with bad argument, but typing should catch this too
732
- { shouldError: true, onError: e => e.message === 'Error parsing field updates: roomId is not valid for updates' }
733
- )
760
+ // currently disabled endpoint altogether
761
+ // await async_test(
762
+ // `update-chat can't update roomId`,
763
+ // () => sdk.api.chats.updateOne(chat.id, { roomId: room.id } as any), // cast to any to allow calling with bad argument, but typing should catch this too
764
+ // { shouldError: true, onError: e => e.message === 'Error parsing field updates: roomId is not valid for updates' }
765
+ // )
734
766
 
735
767
  await sdk.api.chat_rooms.deleteOne(room.id)
736
768
  await wait(undefined, 25)
@@ -760,12 +792,12 @@ const chat_tests = async() => {
760
792
  )
761
793
  await async_test(
762
794
  `get-chats (shared, user1)`,
763
- () => sdk.api.chats.getSome({}, { roomId: sharedRoom.id }),
795
+ () => sdk.api.chats.getSome({ filter: { roomId: sharedRoom.id } }, ),
764
796
  { onResult: cs => cs.length === 2 && !!cs.find(c => c.id === sharedChat.id) && !!cs.find(c => c.id === sharedChat2.id) }
765
797
  )
766
798
  await async_test(
767
799
  `get-chats (shared, user2)`,
768
- () => sdk2.api.chats.getSome({}, { roomId: sharedRoom.id }),
800
+ () => sdk2.api.chats.getSome({ filter: { roomId: sharedRoom.id } }),
769
801
  { onResult: cs => cs.length === 2 && !!cs.find(c => c.id === sharedChat.id) && !!cs.find(c => c.id === sharedChat2.id) }
770
802
  )
771
803
 
@@ -783,6 +815,65 @@ const chat_tests = async() => {
783
815
  )
784
816
  }
785
817
 
818
+ const enduserAccessTests = async () => {
819
+ const email = 'enduser@tellescope.com'
820
+ const password = 'testpassword'
821
+
822
+ const enduser = await sdk.api.endusers.createOne({ email })
823
+ await sdk.api.endusers.setPassword({ id: enduser.id, password }).catch(console.error)
824
+ await enduserSDK.authenticate(email, password).catch(console.error)
825
+
826
+ for (const n in schema) {
827
+ const endpoint = url_safe_path(n)
828
+ const model = schema[n as keyof typeof schema]
829
+
830
+ if (!model?.enduserActions?.read && (model.defaultActions.read || model.customActions.read)) {
831
+ await async_test(
832
+ `no-enduser-access getOne (${endpoint})`,
833
+ () => enduserSDK.GET(`/v1/${endpoint.substring(0, endpoint.length - 1)}/:id`),
834
+ { shouldError: true, onError: (e: string) => e === 'Unauthenticated' }
835
+ )
836
+ }
837
+ if (!model.enduserActions?.readMany && (model.defaultActions.readMany || model.customActions.readMany)) {
838
+ await async_test(
839
+ `no-enduser-access getSome (${endpoint})`,
840
+ () => enduserSDK.GET(`/v1/${endpoint}`),
841
+ { shouldError: true, onError: (e: string) => e === 'Unauthenticated' }
842
+ )
843
+ }
844
+ if (!model.enduserActions?.create && (model.defaultActions.create || model.customActions.create)) {
845
+ await async_test(
846
+ `no-enduser-access createOne (${endpoint})`,
847
+ () => enduserSDK.POST(`/v1/${endpoint.substring(0, endpoint.length - 1)}`),
848
+ { shouldError: true, onError: (e: string) => e === 'Unauthenticated' }
849
+ )
850
+ }
851
+ if (!model.enduserActions?.createMany && (model.defaultActions.createMany || model.customActions.createMany)) {
852
+ await async_test(
853
+ `no-enduser-access createMany (${endpoint})`,
854
+ () => enduserSDK.POST(`/v1/${endpoint}`),
855
+ { shouldError: true, onError: (e: string) => e === 'Unauthenticated' }
856
+ )
857
+ }
858
+ if (!model.enduserActions?.update && (model.defaultActions.update || model.customActions.update)) {
859
+ await async_test(
860
+ `no-enduser-access update (${endpoint})`,
861
+ () => enduserSDK.PATCH(`/v1/${endpoint.substring(0, endpoint.length - 1)}/:id`),
862
+ { shouldError: true, onError: (e: string) => e === 'Unauthenticated' }
863
+ )
864
+ }
865
+ if (!model.enduserActions?.delete && (model.defaultActions.delete || model.customActions.delete)) {
866
+ await async_test(
867
+ `no-enduser-access delete (${endpoint})`,
868
+ () => enduserSDK.DELETE(`/v1/${endpoint.substring(0, endpoint.length - 1)}/:id`),
869
+ { shouldError: true, onError: (e: string) => e === 'Unauthenticated' }
870
+ )
871
+ }
872
+ }
873
+
874
+ await sdk.api.endusers.deleteOne(enduser.id)
875
+ }
876
+
786
877
  const tests: { [K in keyof ClientModelForName]: () => void } = {
787
878
  chats: chat_tests,
788
879
  endusers: enduser_tests,
@@ -795,6 +886,7 @@ const tests: { [K in keyof ClientModelForName]: () => void } = {
795
886
  chat_rooms: chat_room_tests,
796
887
  users: () => {},
797
888
  templates: () => {},
889
+ files: () => {},
798
890
  };
799
891
 
800
892
  (async () => {
@@ -802,6 +894,7 @@ const tests: { [K in keyof ClientModelForName]: () => void } = {
802
894
 
803
895
  log_header("API")
804
896
  await setup_tests()
897
+ await enduserAccessTests()
805
898
  await multi_tenant_tests() // should come right after setup tests
806
899
  await threadKeyTests()
807
900
 
package/tsconfig.json CHANGED
@@ -10,7 +10,6 @@
10
10
  { "path": "../constants" },
11
11
  { "path": "../schema" },
12
12
  { "path": "../testing" },
13
- { "path": "../types" },
14
13
  { "path": "../types-client" },
15
14
  { "path": "../utilities" },
16
15
  { "path": "../validation" }