@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.
- package/build/contexts/api_provider.d.ts.map +1 -1
- package/build/contexts/api_provider.js +3 -1
- package/build/contexts/api_provider.js.map +1 -1
- package/build/utils/client/client.d.ts +23 -0
- package/build/utils/client/client.d.ts.map +1 -0
- package/build/utils/client/client.js +75 -0
- package/build/utils/client/client.js.map +1 -0
- package/build/utils/client/index.d.ts +2 -0
- package/build/utils/client/index.d.ts.map +1 -0
- package/build/utils/client/index.js +2 -0
- package/build/utils/client/index.js.map +1 -0
- package/build/utils/client/request_helpers.d.ts +20 -0
- package/build/utils/client/request_helpers.d.ts.map +1 -0
- package/build/utils/client/request_helpers.js +83 -0
- package/build/utils/client/request_helpers.js.map +1 -0
- package/build/utils/client/transform_request_data.d.ts +8 -0
- package/build/utils/client/transform_request_data.d.ts.map +1 -0
- package/build/utils/client/transform_request_data.js +33 -0
- package/build/utils/client/transform_request_data.js.map +1 -0
- package/build/utils/client/transform_response.d.ts +3 -0
- package/build/utils/client/transform_response.d.ts.map +1 -0
- package/build/utils/client/transform_response.js +84 -0
- package/build/utils/client/transform_response.js.map +1 -0
- package/build/utils/client/utils.d.ts +5 -0
- package/build/utils/client/utils.d.ts.map +1 -0
- package/build/utils/client/utils.js +36 -0
- package/build/utils/client/utils.js.map +1 -0
- package/build/utils/index.d.ts +2 -0
- package/build/utils/index.d.ts.map +1 -1
- package/build/utils/index.js +2 -0
- package/build/utils/index.js.map +1 -1
- package/build/utils/session.d.ts +4 -7
- package/build/utils/session.d.ts.map +1 -1
- package/build/utils/session.js +12 -14
- package/build/utils/session.js.map +1 -1
- package/build/utils/uri.d.ts +21 -0
- package/build/utils/uri.d.ts.map +1 -0
- package/build/utils/uri.js +61 -0
- package/build/utils/uri.js.map +1 -0
- package/package.json +8 -3
- package/src/__tests__/client.ts +388 -0
- package/src/__tests__/{session.tsx → session.ts} +2 -11
- package/src/__utils__/fixtures/defaults/index.ts +17 -0
- package/src/__utils__/fixtures/defaults/records.ts +109 -0
- package/src/__utils__/handlers.ts +38 -0
- package/src/__utils__/server.ts +67 -0
- package/src/contexts/api_provider.tsx +3 -1
- package/src/utils/client/client.ts +114 -0
- package/src/utils/client/index.ts +1 -0
- package/src/utils/client/request_helpers.ts +114 -0
- package/src/utils/client/transform_request_data.ts +48 -0
- package/src/utils/client/transform_response.ts +97 -0
- package/src/utils/client/types.d.ts +64 -0
- package/src/utils/client/utils.ts +45 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/session.ts +12 -15
- 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 }
|
package/src/utils/index.ts
CHANGED
package/src/utils/session.ts
CHANGED
|
@@ -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: '
|
|
7
|
-
staging: '
|
|
8
|
-
development: '
|
|
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 = '
|
|
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
|
|
39
|
-
return
|
|
35
|
+
get host() {
|
|
36
|
+
return this.uri.host
|
|
40
37
|
}
|
|
41
38
|
|
|
42
|
-
get
|
|
43
|
-
return
|
|
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
|
|
57
|
+
export let session = new Session({ env: 'development' })
|
package/src/utils/uri.ts
ADDED
|
@@ -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
|
+
}
|