@planningcenter/chat-react-native 1.5.0-rc.2 → 1.5.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.
Files changed (57) hide show
  1. package/build/contexts/api_provider.d.ts.map +1 -1
  2. package/build/contexts/api_provider.js +3 -1
  3. package/build/contexts/api_provider.js.map +1 -1
  4. package/build/utils/client/client.d.ts +23 -0
  5. package/build/utils/client/client.d.ts.map +1 -0
  6. package/build/utils/client/client.js +75 -0
  7. package/build/utils/client/client.js.map +1 -0
  8. package/build/utils/client/index.d.ts +2 -0
  9. package/build/utils/client/index.d.ts.map +1 -0
  10. package/build/utils/client/index.js +2 -0
  11. package/build/utils/client/index.js.map +1 -0
  12. package/build/utils/client/request_helpers.d.ts +20 -0
  13. package/build/utils/client/request_helpers.d.ts.map +1 -0
  14. package/build/utils/client/request_helpers.js +83 -0
  15. package/build/utils/client/request_helpers.js.map +1 -0
  16. package/build/utils/client/transform_request_data.d.ts +8 -0
  17. package/build/utils/client/transform_request_data.d.ts.map +1 -0
  18. package/build/utils/client/transform_request_data.js +33 -0
  19. package/build/utils/client/transform_request_data.js.map +1 -0
  20. package/build/utils/client/transform_response.d.ts +3 -0
  21. package/build/utils/client/transform_response.d.ts.map +1 -0
  22. package/build/utils/client/transform_response.js +84 -0
  23. package/build/utils/client/transform_response.js.map +1 -0
  24. package/build/utils/client/utils.d.ts +5 -0
  25. package/build/utils/client/utils.d.ts.map +1 -0
  26. package/build/utils/client/utils.js +36 -0
  27. package/build/utils/client/utils.js.map +1 -0
  28. package/build/utils/index.d.ts +2 -0
  29. package/build/utils/index.d.ts.map +1 -1
  30. package/build/utils/index.js +2 -0
  31. package/build/utils/index.js.map +1 -1
  32. package/build/utils/session.d.ts +4 -7
  33. package/build/utils/session.d.ts.map +1 -1
  34. package/build/utils/session.js +12 -14
  35. package/build/utils/session.js.map +1 -1
  36. package/build/utils/uri.d.ts +21 -0
  37. package/build/utils/uri.d.ts.map +1 -0
  38. package/build/utils/uri.js +61 -0
  39. package/build/utils/uri.js.map +1 -0
  40. package/package.json +8 -3
  41. package/src/__tests__/client.ts +388 -0
  42. package/src/__tests__/{session.tsx → session.ts} +2 -11
  43. package/src/__utils__/fixtures/defaults/index.ts +17 -0
  44. package/src/__utils__/fixtures/defaults/records.ts +109 -0
  45. package/src/__utils__/handlers.ts +38 -0
  46. package/src/__utils__/server.ts +67 -0
  47. package/src/contexts/api_provider.tsx +3 -1
  48. package/src/utils/client/client.ts +114 -0
  49. package/src/utils/client/index.ts +1 -0
  50. package/src/utils/client/request_helpers.ts +114 -0
  51. package/src/utils/client/transform_request_data.ts +48 -0
  52. package/src/utils/client/transform_response.ts +97 -0
  53. package/src/utils/client/types.d.ts +64 -0
  54. package/src/utils/client/utils.ts +45 -0
  55. package/src/utils/index.ts +2 -0
  56. package/src/utils/session.ts +12 -15
  57. package/src/utils/uri.ts +70 -0
@@ -0,0 +1,45 @@
1
+ import _ from 'lodash'
2
+
3
+ const toSentence = (words, lastWordConnector = 'and') => {
4
+ lastWordConnector = `${words.length > 2 ? ',' : ''} ${lastWordConnector}`
5
+ return words.join(', ').replace(/,\s([^,]+$)/, `${lastWordConnector} $1`)
6
+ }
7
+
8
+ const toCamel = s => s.replace(/(_[a-z])/g, m => m.toUpperCase().replace('_', ''))
9
+
10
+ const toSnake = s => (s.startsWith('_') ? `_${_.snakeCase(s)}` : _.snakeCase(s))
11
+
12
+ const actualObject = elem => {
13
+ return !((_.isObject(elem) && (_.isArray(elem) || _.isFunction(elem))) || !_.isObject(elem))
14
+ }
15
+
16
+ // map over tree (object) and apply f to each key
17
+ const walker = f => {
18
+ const seen = new WeakMap()
19
+
20
+ const convert = elem => {
21
+ if (actualObject(elem)) {
22
+ return _.reduce(
23
+ elem,
24
+ (memo, obj, key) => {
25
+ if (!seen.has(elem)) {
26
+ seen.set(elem, memo)
27
+ }
28
+ memo[f(key)] = seen.get(obj) || convert(obj)
29
+ return memo
30
+ },
31
+ {}
32
+ )
33
+ } else if (_.isArray(elem)) {
34
+ return _.map(elem, convert)
35
+ } else {
36
+ return elem
37
+ }
38
+ }
39
+ return convert
40
+ }
41
+
42
+ const keysToCamelCase = walker(toCamel)
43
+ const keysToSnakeCase = walker(k => (k < 0 ? k : toSnake(k)))
44
+
45
+ export { keysToCamelCase, keysToSnakeCase, toSentence }
@@ -1,3 +1,5 @@
1
1
  export * from './session'
2
2
  export * from './theme'
3
3
  export * from './styles'
4
+ export * from './client'
5
+ export * from './uri'
@@ -1,17 +1,12 @@
1
1
  import { OAuthToken } from '../types'
2
+ import Uri from './uri'
2
3
 
3
4
  export type ENV = 'production' | 'staging' | 'development'
4
5
 
5
6
  export const baseUrlMap = {
6
- production: 'https://api.planningcenteronline.com',
7
- staging: 'https://api-staging.planningcenteronline.com',
8
- development: 'http://api.pco.test',
9
- }
10
-
11
- export const uploadUrlMap = {
12
- production: 'https://upload.planningcenteronline.com/v2/files',
13
- staging: 'https://upload-staging.planningcenteronline.com/v2/files',
14
- development: 'https://upload.planningcenteronline.com/v2/files',
7
+ production: 'api.planningcenteronline.com',
8
+ staging: 'api-staging.planningcenteronline.com',
9
+ development: 'api.pco.test',
15
10
  }
16
11
 
17
12
  type SessionProps = { env?: ENV; token?: OAuthToken } | undefined
@@ -24,23 +19,25 @@ type SessionProps = { env?: ENV; token?: OAuthToken } | undefined
24
19
  export class Session {
25
20
  env: ENV
26
21
  token: OAuthToken | undefined
22
+ uri: Uri
27
23
 
28
24
  constructor(props?: SessionProps) {
29
- const { env = 'development', token } = props || {}
25
+ const { env = 'production', token } = props || {}
30
26
  this.env = env
31
27
  this.token = token
28
+ this.uri = new Uri({ session: this })
32
29
  }
33
30
 
34
31
  get isAuthenticated() {
35
32
  return Boolean(this.token)
36
33
  }
37
34
 
38
- get baseUrl() {
39
- return baseUrlMap[this.env]
35
+ get host() {
36
+ return this.uri.host
40
37
  }
41
38
 
42
- get uploadUrl() {
43
- return uploadUrlMap[this.env]
39
+ get baseUrl() {
40
+ return this.uri.baseUrl
44
41
  }
45
42
 
46
43
  toString() {
@@ -57,4 +54,4 @@ export class Session {
57
54
  }
58
55
  }
59
56
 
60
- export const session = new Session({ env: 'development' })
57
+ export let session = new Session({ env: 'development' })
@@ -0,0 +1,70 @@
1
+ import DeviceInfo from 'react-native-device-info'
2
+ import { Session } from './session'
3
+ const brand = DeviceInfo.getBrand()
4
+ const model = DeviceInfo.getModel()
5
+ const systemName = DeviceInfo.getSystemName()
6
+ const systemVersion = DeviceInfo.getSystemVersion()
7
+ const readableVersion = DeviceInfo.getReadableVersion()
8
+ const appName = DeviceInfo.getApplicationName()
9
+
10
+ export default class Uri {
11
+ session: Session
12
+ app?: string
13
+
14
+ constructor({ session, app }: { session: Session; app?: string }) {
15
+ this.session = session
16
+ this.app = app
17
+ }
18
+
19
+ get schema() {
20
+ if (this.env === 'development') {
21
+ return 'http'
22
+ } else {
23
+ return 'https'
24
+ }
25
+ }
26
+
27
+ get host() {
28
+ switch (this.env) {
29
+ case 'production':
30
+ return 'api.planningcenteronline.com'
31
+ case 'staging':
32
+ return 'api-staging.planningcenteronline.com'
33
+ case 'development':
34
+ return 'api.pco.test'
35
+ default:
36
+ return 'api.planningcenteronline.com'
37
+ }
38
+ }
39
+
40
+ get env() {
41
+ return this.session?.env || 'production'
42
+ }
43
+
44
+ get baseUrl() {
45
+ return `${this.schema}://${this.host}`
46
+ }
47
+
48
+ get directory() {
49
+ return this.app ? `/${this.app}/v2` : ''
50
+ }
51
+
52
+ get headers() {
53
+ return {
54
+ 'User-Agent': `${appName}/${readableVersion} (${brand}, ${model}, ${systemName}, ${systemVersion})`,
55
+ Authorization: `Bearer ${this.session.token?.access_token}`,
56
+ }
57
+ }
58
+
59
+ appUrl = path => {
60
+ if (path.startsWith(`${this.schema}://`)) return path
61
+
62
+ return `${this.baseUrl}${this.directory}${path || '/'}`
63
+ }
64
+
65
+ api = path => {
66
+ if (path.startsWith(`${this.schema}://`)) return path
67
+
68
+ return `${this.baseUrl}${path || '/'}`
69
+ }
70
+ }