@planningcenter/chat-react-native 3.2.0-rc.15 → 3.2.0-rc.17
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 +1 -1
- package/build/contexts/api_provider.d.ts.map +1 -1
- package/build/contexts/api_provider.js +3 -3
- package/build/contexts/api_provider.js.map +1 -1
- package/build/contexts/chat_context.d.ts +4 -4
- package/build/contexts/chat_context.d.ts.map +1 -1
- package/build/contexts/chat_context.js +3 -3
- package/build/contexts/chat_context.js.map +1 -1
- package/build/hooks/use_api.d.ts.map +1 -1
- package/build/hooks/use_api.js +5 -0
- package/build/hooks/use_api.js.map +1 -1
- package/build/hooks/use_api_client.d.ts.map +1 -1
- package/build/hooks/use_api_client.js +5 -3
- package/build/hooks/use_api_client.js.map +1 -1
- package/build/hooks/use_jolt.d.ts.map +1 -1
- package/build/hooks/use_jolt.js +15 -22
- package/build/hooks/use_jolt.js.map +1 -1
- package/build/hooks/use_message_create.js +2 -2
- package/build/hooks/use_message_create.js.map +1 -1
- package/build/hooks/use_suspense_api.d.ts.map +1 -1
- package/build/hooks/use_suspense_api.js +5 -0
- package/build/hooks/use_suspense_api.js.map +1 -1
- package/build/index.d.ts +2 -2
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2 -2
- package/build/index.js.map +1 -1
- package/build/types/resources/oauth_token.d.ts +4 -4
- package/build/types/resources/oauth_token.d.ts.map +1 -1
- package/build/types/resources/oauth_token.js.map +1 -1
- package/build/utils/client/client.d.ts +4 -8
- package/build/utils/client/client.d.ts.map +1 -1
- package/build/utils/client/client.js +10 -9
- package/build/utils/client/client.js.map +1 -1
- package/build/utils/session.d.ts +6 -2
- package/build/utils/session.d.ts.map +1 -1
- package/build/utils/session.js +6 -1
- package/build/utils/session.js.map +1 -1
- package/build/utils/uri.d.ts +10 -2
- package/build/utils/uri.d.ts.map +1 -1
- package/build/utils/uri.js +24 -6
- package/build/utils/uri.js.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/hooks/useTheme.tsx +1 -1
- package/src/__tests__/{client.ts → utils/client.ts} +7 -115
- package/src/__tests__/{session.ts → utils/session.ts} +4 -4
- package/src/__tests__/utils/uri.ts +107 -0
- package/src/contexts/api_provider.tsx +3 -3
- package/src/contexts/chat_context.tsx +7 -7
- package/src/hooks/use_api.ts +8 -2
- package/src/hooks/use_api_client.ts +7 -3
- package/src/hooks/use_jolt.ts +18 -23
- package/src/hooks/use_message_create.ts +2 -2
- package/src/hooks/use_suspense_api.ts +6 -0
- package/src/index.tsx +2 -1
- package/src/types/resources/oauth_token.ts +4 -4
- package/src/utils/client/client.ts +13 -13
- package/src/utils/session.ts +10 -4
- package/src/utils/uri.ts +30 -6
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/utils/client/client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/utils/client/client.ts"],"names":[],"mappings":"AACA,OAAO,EACL,aAAa,EACb,wBAAwB,EACxB,WAAW,EAEX,yBAAyB,EACzB,uBAAuB,GACxB,MAAM,mBAAmB,CAAA;AAiB1B,MAAM,OAAO,MAAM;IACjB,OAAO,GAAW,EAAE,CAAA;IACpB,cAAc,GAA2B,EAAE,CAAA;IAC3C,sBAAsB,CAAyB;IAC/C,IAAI,CAAS;IAEb,YAAY,EAAE,OAAO,EAAE,cAAc,GAAG,EAAE,EAAE,sBAAsB,EAAE,IAAI,EAAc;QACpF,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,cAAc,GAAG,cAAc,CAAA;QACpC,IAAI,CAAC,sBAAsB,GAAG,sBAAsB,CAAA;QACpD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;IAED,KAAK,CAAC,GAAG,CAAwC,IAAgB;QAC/D,MAAM,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,GAAG,IAAI,C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;QACX,CAAC;IACH,CAAC,CAAA;IAED,MAAM,CAAC,GAAW;QAChB,OAAO,GAAG,IAAI,CAAC,IAAI,GAAG,GAAG,EAAE,CAAA;IAC7B,CAAC;IAED,IAAI,OAAO;QACT,OAAO;YACL,MAAM,EAAE,0BAA0B;YAClC,cAAc,EAAE,kBAAkB;YAClC,mBAAmB,EAAE,IAAI,CAAC,OAAO;YACjC,GAAG,IAAI,CAAC,cAAc;SACvB,CAAA;IACH,CAAC;CACF;AAED,eAAe,MAAM,CAAA","sourcesContent":["import { ApiCollection, ApiError, ApiResource } from '../../types'\nimport {\n concatRecords,\n ensureNoQueryParamsInDev,\n makeRequest,\n MakeRequestArgs,\n throwErrorIfFieldsMissing,\n throwErrorIfQueryParams,\n} from './request_helpers'\n\nimport { DeleteRequest, GetRequest, PatchRequest, PostRequest, WalkRequest } from './types'\n\nexport interface ResponseError extends Response {\n errors: ApiError['errors']\n}\n\nexport type OnUnauthorizedResponse = (_response: ResponseError) => void\n\ntype ClientArgs = {\n version: string\n defaultHeaders?: Record<string, string>\n onUnauthorizedResponse?: OnUnauthorizedResponse\n root?: string\n}\n\nexport class Client {\n version: string = ''\n defaultHeaders: Record<string, string> = {}\n onUnauthorizedResponse?: OnUnauthorizedResponse\n root?: string\n\n constructor({ version, defaultHeaders = {}, onUnauthorizedResponse, root }: ClientArgs) {\n this.version = version\n this.defaultHeaders = defaultHeaders\n this.onUnauthorizedResponse = onUnauthorizedResponse\n this.root = root\n }\n\n async get<T extends ApiCollection | ApiResource>(args: GetRequest): Promise<T> {\n const { walk, ...data } = args.data\n const isWalking = Boolean(walk)\n const headers = { ...this.headers, ...args.headers }\n const url = this.appUrl(args.url)\n\n await throwErrorIfQueryParams(url)\n\n const requestArgs: MakeRequestArgs = { data, url, action: 'GET', headers }\n\n const walkRequest = ({\n url: requestUrl,\n data: d = { fields: {} },\n acc = { data: [], included: [], meta: {}, links: {} },\n ...options\n }: WalkRequest): Promise<T> => {\n return makeRequest({\n action: 'GET',\n data: d,\n url: requestUrl,\n ...options,\n headers,\n }).then(({ links, ...rest }) => {\n const records = Array.isArray(rest.data) ? concatRecords(acc, rest) : rest\n\n // `next` will have our params in the link so we do not want to pass them back in\n if (links?.next) {\n return walkRequest({ ...options, data: d, url: links.next, acc: records })\n } else {\n return records\n }\n })\n }\n\n const handler = isWalking\n ? (a: MakeRequestArgs | WalkRequest) => walkRequest(a as WalkRequest)\n : (a: MakeRequestArgs | WalkRequest) => makeRequest(a as MakeRequestArgs)\n\n return throwErrorIfFieldsMissing(handler, requestArgs)\n .then(response => response as T)\n .catch(this.handleNotOk)\n }\n\n async patch<T extends ApiCollection | ApiResource>(args: PatchRequest): Promise<T> {\n const headers = { ...this.headers, ...args.headers }\n const url = this.appUrl(args.url)\n\n const requestArgs: MakeRequestArgs = { data: args.data, url, action: 'PATCH', headers }\n\n return ensureNoQueryParamsInDev(makeRequest, requestArgs)\n .then(response => response as T)\n .catch(this.handleNotOk)\n }\n\n async post<T extends ApiCollection | ApiResource>(args: PostRequest): Promise<T> {\n const headers = { ...this.headers, ...args.headers }\n const url = this.appUrl(args.url)\n\n const requestArgs: MakeRequestArgs = { ...args, data: args.data, url, action: 'POST', headers }\n\n return ensureNoQueryParamsInDev(makeRequest, requestArgs)\n .then(response => response as T)\n .catch(this.handleNotOk)\n }\n\n async delete(args: DeleteRequest) {\n const headers = { ...this.headers, ...args.headers }\n const url = this.appUrl(args.url)\n\n const requestArgs: MakeRequestArgs = { url, action: 'DELETE', headers }\n\n return makeRequest(requestArgs).catch(this.handleNotOk)\n }\n\n handleNotOk = async (response: Response) => {\n if (response.status === 401) {\n const { errors } = await this.parseErrorResponse(response)\n this.onUnauthorizedResponse?.({\n ...response,\n errors: errors || [],\n })\n }\n\n return Promise.reject(response)\n }\n\n parseErrorResponse = async (response: Response): Promise<Partial<ApiError>> => {\n try {\n return (await response.json()) as ApiError\n } catch {\n return {}\n }\n }\n\n appUrl(url: string) {\n return `${this.root}${url}`\n }\n\n get headers() {\n return {\n Accept: 'application/vnd.api+json',\n 'Content-Type': 'application/json',\n 'X-PCO-API-Version': this.version,\n ...this.defaultHeaders,\n }\n }\n}\n\nexport default Client\n"]}
|
package/build/utils/session.d.ts
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
import { OAuthToken } from '../types';
|
|
2
2
|
export type ENV = 'production' | 'staging' | 'development';
|
|
3
|
+
export type OauthType = 'OAuth' | 'ChurchCenterOauth';
|
|
3
4
|
export type PartialToken = Pick<OAuthToken, 'access_token'> & Partial<OAuthToken>;
|
|
4
|
-
export type SessionProps<T
|
|
5
|
+
export type SessionProps<T extends PartialToken> = {
|
|
5
6
|
env?: ENV;
|
|
6
7
|
token?: T;
|
|
8
|
+
type?: OauthType;
|
|
7
9
|
};
|
|
8
10
|
/**
|
|
9
11
|
* Session class to track the environment and token
|
|
10
12
|
* Not intended to make network requests or handle authentication
|
|
11
13
|
*/
|
|
12
|
-
export declare class Session<T = PartialToken> {
|
|
14
|
+
export declare class Session<T extends PartialToken = PartialToken> {
|
|
13
15
|
env: ENV;
|
|
14
16
|
token: T | undefined;
|
|
17
|
+
type: OauthType;
|
|
15
18
|
constructor(props?: SessionProps<T>);
|
|
16
19
|
get isAuthenticated(): boolean;
|
|
20
|
+
get isChurchCenterToken(): boolean;
|
|
17
21
|
toString(): string;
|
|
18
22
|
static hydrate<T extends PartialToken>(sessionString: string): Session<T>;
|
|
19
23
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/utils/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAErC,MAAM,MAAM,GAAG,GAAG,YAAY,GAAG,SAAS,GAAG,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/utils/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAErC,MAAM,MAAM,GAAG,GAAG,YAAY,GAAG,SAAS,GAAG,aAAa,CAAA;AAC1D,MAAM,MAAM,SAAS,GAAG,OAAO,GAAG,mBAAmB,CAAA;AACrD,MAAM,MAAM,YAAY,GAAG,IAAI,CAAC,UAAU,EAAE,cAAc,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAA;AACjF,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,YAAY,IAAI;IAAE,GAAG,CAAC,EAAE,GAAG,CAAC;IAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IAAC,IAAI,CAAC,EAAE,SAAS,CAAA;CAAE,CAAA;AAE7F;;;GAGG;AACH,qBAAa,OAAO,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY;IACxD,GAAG,EAAE,GAAG,CAAA;IACR,KAAK,EAAE,CAAC,GAAG,SAAS,CAAA;IACpB,IAAI,EAAE,SAAS,CAAA;gBAEH,KAAK,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC;IAOnC,IAAI,eAAe,YAElB;IAED,IAAI,mBAAmB,YAEtB;IAED,QAAQ;IAIR,MAAM,CAAC,OAAO,CAAC,CAAC,SAAS,YAAY,EAAE,aAAa,EAAE,MAAM;CAQ7D"}
|
package/build/utils/session.js
CHANGED
|
@@ -5,14 +5,19 @@
|
|
|
5
5
|
export class Session {
|
|
6
6
|
env;
|
|
7
7
|
token;
|
|
8
|
+
type;
|
|
8
9
|
constructor(props) {
|
|
9
|
-
const { env = 'production', token } = props || {};
|
|
10
|
+
const { env = 'production', token, type } = props || {};
|
|
10
11
|
this.env = env;
|
|
11
12
|
this.token = token;
|
|
13
|
+
this.type = type || 'OAuth';
|
|
12
14
|
}
|
|
13
15
|
get isAuthenticated() {
|
|
14
16
|
return Boolean(this.token);
|
|
15
17
|
}
|
|
18
|
+
get isChurchCenterToken() {
|
|
19
|
+
return this.type === 'ChurchCenterOauth';
|
|
20
|
+
}
|
|
16
21
|
toString() {
|
|
17
22
|
return JSON.stringify({ env: this.env, token: this.token });
|
|
18
23
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/utils/session.ts"],"names":[],"mappings":"AAOA;;;GAGG;AACH,MAAM,OAAO,OAAO;IAClB,GAAG,CAAK;IACR,KAAK,CAAe;
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/utils/session.ts"],"names":[],"mappings":"AAOA;;;GAGG;AACH,MAAM,OAAO,OAAO;IAClB,GAAG,CAAK;IACR,KAAK,CAAe;IACpB,IAAI,CAAW;IAEf,YAAY,KAAuB;QACjC,MAAM,EAAE,GAAG,GAAG,YAAY,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,IAAI,EAAE,CAAA;QACvD,IAAI,CAAC,GAAG,GAAG,GAAG,CAAA;QACd,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAClB,IAAI,CAAC,IAAI,GAAG,IAAI,IAAI,OAAO,CAAA;IAC7B,CAAC;IAED,IAAI,eAAe;QACjB,OAAO,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IAC5B,CAAC;IAED,IAAI,mBAAmB;QACrB,OAAO,IAAI,CAAC,IAAI,KAAK,mBAAmB,CAAA;IAC1C,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC,CAAA;IAC7D,CAAC;IAED,MAAM,CAAC,OAAO,CAAyB,aAAqB;QAC1D,IAAI,CAAC;YACH,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,CAAA;YACvC,OAAO,IAAI,OAAO,CAAI,KAAK,CAAC,CAAA;QAC9B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,IAAI,OAAO,EAAK,CAAA;QACzB,CAAC;IACH,CAAC;CACF","sourcesContent":["import { OAuthToken } from '../types'\n\nexport type ENV = 'production' | 'staging' | 'development'\nexport type OauthType = 'OAuth' | 'ChurchCenterOauth'\nexport type PartialToken = Pick<OAuthToken, 'access_token'> & Partial<OAuthToken>\nexport type SessionProps<T extends PartialToken> = { env?: ENV; token?: T; type?: OauthType }\n\n/**\n * Session class to track the environment and token\n * Not intended to make network requests or handle authentication\n */\nexport class Session<T extends PartialToken = PartialToken> {\n env: ENV\n token: T | undefined\n type: OauthType\n\n constructor(props?: SessionProps<T>) {\n const { env = 'production', token, type } = props || {}\n this.env = env\n this.token = token\n this.type = type || 'OAuth'\n }\n\n get isAuthenticated() {\n return Boolean(this.token)\n }\n\n get isChurchCenterToken() {\n return this.type === 'ChurchCenterOauth'\n }\n\n toString() {\n return JSON.stringify({ env: this.env, token: this.token })\n }\n\n static hydrate<T extends PartialToken>(sessionString: string) {\n try {\n const props = JSON.parse(sessionString)\n return new Session<T>(props)\n } catch (error) {\n return new Session<T>()\n }\n }\n}\n"]}
|
package/build/utils/uri.d.ts
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import { Session } from './session';
|
|
2
|
+
type Graph = 'churchcenter' | 'planningcenter';
|
|
2
3
|
export declare class Uri {
|
|
3
4
|
session: Session;
|
|
5
|
+
graph: Graph;
|
|
4
6
|
app?: string;
|
|
5
|
-
constructor({ session, app }: {
|
|
7
|
+
constructor({ session, app, graph }: {
|
|
6
8
|
session: Session;
|
|
7
9
|
app?: string;
|
|
10
|
+
graph?: Graph;
|
|
8
11
|
});
|
|
9
12
|
get schema(): "http" | "https";
|
|
10
|
-
get host():
|
|
13
|
+
get host(): string;
|
|
14
|
+
get subdomain(): "api.staging" | "api-staging" | "api";
|
|
15
|
+
get domain(): 'pco' | 'planningcenteronline' | 'churchcenter';
|
|
16
|
+
get tld(): "test" | "com";
|
|
17
|
+
get isChurchCenter(): boolean;
|
|
11
18
|
get env(): import("./session").ENV;
|
|
12
19
|
get baseUrl(): string;
|
|
13
20
|
get directory(): string;
|
|
@@ -18,4 +25,5 @@ export declare class Uri {
|
|
|
18
25
|
appUrl: (path: string) => string;
|
|
19
26
|
api: (path: string) => string;
|
|
20
27
|
}
|
|
28
|
+
export {};
|
|
21
29
|
//# sourceMappingURL=uri.d.ts.map
|
package/build/utils/uri.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uri.d.ts","sourceRoot":"","sources":["../../src/utils/uri.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAQnC,qBAAa,GAAG;IACd,OAAO,EAAE,OAAO,CAAA;IAChB,GAAG,CAAC,EAAE,MAAM,CAAA;gBAEA,EAAE,OAAO,EAAE,GAAG,EAAE,EAAE;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,CAAA;KAAE;
|
|
1
|
+
{"version":3,"file":"uri.d.ts","sourceRoot":"","sources":["../../src/utils/uri.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAQnC,KAAK,KAAK,GAAG,cAAc,GAAG,gBAAgB,CAAA;AAC9C,qBAAa,GAAG;IACd,OAAO,EAAE,OAAO,CAAA;IAChB,KAAK,EAAE,KAAK,CAAmB;IAC/B,GAAG,CAAC,EAAE,MAAM,CAAA;gBAEA,EAAE,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,KAAK,CAAA;KAAE;IAMtF,IAAI,MAAM,qBAMT;IAED,IAAI,IAAI,WAEP;IAED,IAAI,SAAS,0CAOZ;IAED,IAAI,MAAM,IAAI,KAAK,GAAG,sBAAsB,GAAG,cAAc,CAM5D;IAED,IAAI,GAAG,mBAON;IAED,IAAI,cAAc,YAEjB;IAED,IAAI,GAAG,4BAEN;IAED,IAAI,OAAO,WAEV;IAED,IAAI,SAAS,WAEZ;IAED,IAAI,OAAO;;;MAKV;IAED,MAAM,SAAU,MAAM,YAIrB;IAED,GAAG,SAAU,MAAM,YAIlB;CACF"}
|
package/build/utils/uri.js
CHANGED
|
@@ -7,10 +7,12 @@ const readableVersion = DeviceInfo.getReadableVersion();
|
|
|
7
7
|
const appName = DeviceInfo.getApplicationName();
|
|
8
8
|
export class Uri {
|
|
9
9
|
session;
|
|
10
|
+
graph = 'planningcenter';
|
|
10
11
|
app;
|
|
11
|
-
constructor({ session, app }) {
|
|
12
|
+
constructor({ session, app, graph }) {
|
|
12
13
|
this.session = session;
|
|
13
14
|
this.app = app;
|
|
15
|
+
this.graph = graph || 'planningcenter';
|
|
14
16
|
}
|
|
15
17
|
get schema() {
|
|
16
18
|
if (this.env === 'development') {
|
|
@@ -21,17 +23,33 @@ export class Uri {
|
|
|
21
23
|
}
|
|
22
24
|
}
|
|
23
25
|
get host() {
|
|
26
|
+
return `${this.subdomain}.${this.domain}.${this.tld}`;
|
|
27
|
+
}
|
|
28
|
+
get subdomain() {
|
|
24
29
|
switch (this.env) {
|
|
25
|
-
case 'production':
|
|
26
|
-
return 'api.planningcenteronline.com';
|
|
27
30
|
case 'staging':
|
|
28
|
-
return 'api-staging
|
|
31
|
+
return this.isChurchCenter ? 'api.staging' : 'api-staging';
|
|
32
|
+
default:
|
|
33
|
+
return 'api';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
get domain() {
|
|
37
|
+
if (this.isChurchCenter) {
|
|
38
|
+
return 'churchcenter';
|
|
39
|
+
}
|
|
40
|
+
return this.env === 'development' ? 'pco' : 'planningcenteronline';
|
|
41
|
+
}
|
|
42
|
+
get tld() {
|
|
43
|
+
switch (this.env) {
|
|
29
44
|
case 'development':
|
|
30
|
-
return '
|
|
45
|
+
return 'test';
|
|
31
46
|
default:
|
|
32
|
-
return '
|
|
47
|
+
return 'com';
|
|
33
48
|
}
|
|
34
49
|
}
|
|
50
|
+
get isChurchCenter() {
|
|
51
|
+
return this.session.isChurchCenterToken;
|
|
52
|
+
}
|
|
35
53
|
get env() {
|
|
36
54
|
return this.session?.env || 'production';
|
|
37
55
|
}
|
package/build/utils/uri.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uri.js","sourceRoot":"","sources":["../../src/utils/uri.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,0BAA0B,CAAA;AAEjD,MAAM,KAAK,GAAG,UAAU,CAAC,QAAQ,EAAE,CAAA;AACnC,MAAM,KAAK,GAAG,UAAU,CAAC,QAAQ,EAAE,CAAA;AACnC,MAAM,UAAU,GAAG,UAAU,CAAC,aAAa,EAAE,CAAA;AAC7C,MAAM,aAAa,GAAG,UAAU,CAAC,gBAAgB,EAAE,CAAA;AACnD,MAAM,eAAe,GAAG,UAAU,CAAC,kBAAkB,EAAE,CAAA;AACvD,MAAM,OAAO,GAAG,UAAU,CAAC,kBAAkB,EAAE,CAAA;
|
|
1
|
+
{"version":3,"file":"uri.js","sourceRoot":"","sources":["../../src/utils/uri.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,0BAA0B,CAAA;AAEjD,MAAM,KAAK,GAAG,UAAU,CAAC,QAAQ,EAAE,CAAA;AACnC,MAAM,KAAK,GAAG,UAAU,CAAC,QAAQ,EAAE,CAAA;AACnC,MAAM,UAAU,GAAG,UAAU,CAAC,aAAa,EAAE,CAAA;AAC7C,MAAM,aAAa,GAAG,UAAU,CAAC,gBAAgB,EAAE,CAAA;AACnD,MAAM,eAAe,GAAG,UAAU,CAAC,kBAAkB,EAAE,CAAA;AACvD,MAAM,OAAO,GAAG,UAAU,CAAC,kBAAkB,EAAE,CAAA;AAG/C,MAAM,OAAO,GAAG;IACd,OAAO,CAAS;IAChB,KAAK,GAAU,gBAAgB,CAAA;IAC/B,GAAG,CAAS;IAEZ,YAAY,EAAE,OAAO,EAAE,GAAG,EAAE,KAAK,EAAqD;QACpF,IAAI,CAAC,OAAO,GAAG,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,CAAA;IACxC,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE,CAAA;IAC1C,CAAC;IAED,IAAI,OAAO;QACT,OAAO;YACL,YAAY,EAAE,GAAG,OAAO,IAAI,eAAe,KAAK,KAAK,KAAK,KAAK,KAAK,UAAU,KAAK,aAAa,GAAG;YACnG,aAAa,EAAE,UAAU,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,YAAY,EAAE;SAC5D,CAAA;IACH,CAAC;IAED,MAAM,GAAG,CAAC,IAAY,EAAE,EAAE;QACxB,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,IAAI,CAAA;QAErD,OAAO,GAAG,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,SAAS,GAAG,IAAI,IAAI,GAAG,EAAE,CAAA;IACzD,CAAC,CAAA;IAED,GAAG,GAAG,CAAC,IAAY,EAAE,EAAE;QACrB,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,IAAI,CAAA;QAErD,OAAO,GAAG,IAAI,CAAC,OAAO,GAAG,IAAI,IAAI,GAAG,EAAE,CAAA;IACxC,CAAC,CAAA;CACF","sourcesContent":["import DeviceInfo from 'react-native-device-info'\nimport { Session } from './session'\nconst brand = DeviceInfo.getBrand()\nconst model = DeviceInfo.getModel()\nconst systemName = DeviceInfo.getSystemName()\nconst systemVersion = DeviceInfo.getSystemVersion()\nconst readableVersion = DeviceInfo.getReadableVersion()\nconst appName = DeviceInfo.getApplicationName()\n\ntype Graph = 'churchcenter' | 'planningcenter'\nexport class Uri {\n session: Session\n graph: Graph = 'planningcenter'\n app?: string\n\n constructor({ session, app, graph }: { session: Session; app?: string; graph?: Graph }) {\n this.session = session\n this.app = app\n this.graph = graph || 'planningcenter'\n }\n\n get schema() {\n if (this.env === 'development') {\n return 'http'\n } else {\n return 'https'\n }\n }\n\n get host() {\n return `${this.subdomain}.${this.domain}.${this.tld}`\n }\n\n get subdomain() {\n switch (this.env) {\n case 'staging':\n return this.isChurchCenter ? 'api.staging' : 'api-staging'\n default:\n return 'api'\n }\n }\n\n get domain(): 'pco' | 'planningcenteronline' | 'churchcenter' {\n if (this.isChurchCenter) {\n return 'churchcenter'\n }\n\n return this.env === 'development' ? 'pco' : 'planningcenteronline'\n }\n\n get tld() {\n switch (this.env) {\n case 'development':\n return 'test'\n default:\n return 'com'\n }\n }\n\n get isChurchCenter() {\n return this.session.isChurchCenterToken\n }\n\n get env() {\n return this.session?.env || 'production'\n }\n\n get baseUrl() {\n return `${this.schema}://${this.host}`\n }\n\n get directory() {\n return this.app ? `/${this.app}/v2` : ''\n }\n\n get headers() {\n return {\n 'User-Agent': `${appName}/${readableVersion} (${brand}, ${model}, ${systemName}, ${systemVersion})`,\n Authorization: `Bearer ${this.session.token?.access_token}`,\n }\n }\n\n appUrl = (path: string) => {\n if (path.startsWith(`${this.schema}://`)) return path\n\n return `${this.baseUrl}${this.directory}${path || '/'}`\n }\n\n api = (path: string) => {\n if (path.startsWith(`${this.schema}://`)) return path\n\n return `${this.baseUrl}${path || '/'}`\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@planningcenter/chat-react-native",
|
|
3
|
-
"version": "3.2.0-rc.
|
|
3
|
+
"version": "3.2.0-rc.17",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"prettier": "^3.4.2",
|
|
56
56
|
"typescript": "<5.6.0"
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "e08fb91663287cbdec079e616e66908c09535bb8"
|
|
59
59
|
}
|
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
import MockServer from '
|
|
2
|
-
import Client from '
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import { ApiError, OAuthToken } from '../types'
|
|
1
|
+
import MockServer from '../../__utils__/server'
|
|
2
|
+
import Client from '../../utils/client/client'
|
|
3
|
+
import DefaultFixtures from '../../__utils__/fixtures/defaults'
|
|
4
|
+
import { BASE_URL } from '../../__utils__/handlers'
|
|
5
|
+
import { ApiError } from '../../types'
|
|
7
6
|
|
|
8
7
|
const APP_BASE_URL = BASE_URL
|
|
9
|
-
|
|
10
|
-
let session: Session
|
|
11
8
|
let client: Client
|
|
12
9
|
let clientWithDefaultHeaders: Client
|
|
13
10
|
let fetchSpy: jest.SpyInstance
|
|
@@ -21,20 +18,17 @@ const onUnauthorizedResponse = jest.fn()
|
|
|
21
18
|
beforeEach(() => {
|
|
22
19
|
jest.clearAllMocks()
|
|
23
20
|
fetchSpy = jest.spyOn(globalThis, 'fetch')
|
|
24
|
-
session = new Session({ env: 'development' })
|
|
25
21
|
client = new Client({
|
|
26
|
-
app: 'chat',
|
|
27
22
|
version: '2018-11-01',
|
|
28
|
-
|
|
23
|
+
root: APP_BASE_URL,
|
|
29
24
|
onUnauthorizedResponse,
|
|
30
25
|
})
|
|
31
26
|
clientWithDefaultHeaders = new Client({
|
|
32
|
-
app: 'chat',
|
|
33
27
|
version: '2018-11-01',
|
|
28
|
+
root: APP_BASE_URL,
|
|
34
29
|
defaultHeaders: {
|
|
35
30
|
'X-Custom-Default-Header': 'important data',
|
|
36
31
|
},
|
|
37
|
-
session,
|
|
38
32
|
onUnauthorizedResponse,
|
|
39
33
|
})
|
|
40
34
|
})
|
|
@@ -345,96 +339,6 @@ describe('delete', () => {
|
|
|
345
339
|
})
|
|
346
340
|
})
|
|
347
341
|
|
|
348
|
-
describe('url switching', () => {
|
|
349
|
-
it('Changes to session switches the base url', async () => {
|
|
350
|
-
const devBase = 'http://api.pco.test/chat/v2'
|
|
351
|
-
const prodBase = 'https://api.planningcenteronline.com/chat/v2'
|
|
352
|
-
const stagingBase = 'https://api-staging.planningcenteronline.com/chat/v2'
|
|
353
|
-
|
|
354
|
-
MockServer.get(prodBase + '/records', {}, 200, { once: true })
|
|
355
|
-
MockServer.get(stagingBase + '/records', {}, 200, { once: true })
|
|
356
|
-
|
|
357
|
-
const newClient = new Client({
|
|
358
|
-
app: 'chat',
|
|
359
|
-
session,
|
|
360
|
-
version: '2018-11-01',
|
|
361
|
-
onUnauthorizedResponse: () => {},
|
|
362
|
-
})
|
|
363
|
-
|
|
364
|
-
await newClient.get({
|
|
365
|
-
url: '/records',
|
|
366
|
-
data: { fields: { Record: ['id'] } },
|
|
367
|
-
})
|
|
368
|
-
|
|
369
|
-
expect(fetchSpy).toHaveBeenCalledWith(
|
|
370
|
-
devBase + '/records?fields[Record]=id&per_page=100',
|
|
371
|
-
expect.any(Object)
|
|
372
|
-
)
|
|
373
|
-
|
|
374
|
-
session.env = 'staging'
|
|
375
|
-
|
|
376
|
-
await newClient.get({
|
|
377
|
-
url: '/records',
|
|
378
|
-
data: { fields: { Record: ['id'] } },
|
|
379
|
-
})
|
|
380
|
-
|
|
381
|
-
expect(fetchSpy).toHaveBeenLastCalledWith(
|
|
382
|
-
'https://api-staging.planningcenteronline.com/chat/v2' +
|
|
383
|
-
'/records?fields[Record]=id&per_page=100',
|
|
384
|
-
expect.any(Object)
|
|
385
|
-
)
|
|
386
|
-
|
|
387
|
-
session.env = 'production'
|
|
388
|
-
|
|
389
|
-
await newClient.get({
|
|
390
|
-
url: '/records',
|
|
391
|
-
data: { fields: { Record: ['id'] } },
|
|
392
|
-
})
|
|
393
|
-
|
|
394
|
-
expect(fetchSpy).toHaveBeenLastCalledWith(
|
|
395
|
-
'https://api.planningcenteronline.com/chat/v2' + '/records?fields[Record]=id&per_page=100',
|
|
396
|
-
expect.any(Object)
|
|
397
|
-
)
|
|
398
|
-
})
|
|
399
|
-
|
|
400
|
-
it('Changes to session switches the base url with custom headers', async () => {
|
|
401
|
-
const token: OAuthToken = {
|
|
402
|
-
access_token: 'foo',
|
|
403
|
-
token_type: undefined,
|
|
404
|
-
created_at: 0,
|
|
405
|
-
expires_in: undefined,
|
|
406
|
-
scope: '',
|
|
407
|
-
refresh_token: undefined,
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
session.token = token
|
|
411
|
-
|
|
412
|
-
await client.get({
|
|
413
|
-
url: '/records',
|
|
414
|
-
data: { fields: { Record: ['id'] } },
|
|
415
|
-
})
|
|
416
|
-
|
|
417
|
-
requestHeadersShouldContain({
|
|
418
|
-
...fetchSpy.mock.calls[0][1],
|
|
419
|
-
key: 'Authorization',
|
|
420
|
-
value: 'foo',
|
|
421
|
-
})
|
|
422
|
-
|
|
423
|
-
session.token.access_token = 'bar'
|
|
424
|
-
|
|
425
|
-
await client.get({
|
|
426
|
-
url: '/records',
|
|
427
|
-
data: { fields: { Record: ['id'] } },
|
|
428
|
-
})
|
|
429
|
-
|
|
430
|
-
requestHeadersShouldContain({
|
|
431
|
-
...fetchSpy.mock.calls[1][1],
|
|
432
|
-
key: 'Authorization',
|
|
433
|
-
value: 'bar',
|
|
434
|
-
})
|
|
435
|
-
})
|
|
436
|
-
})
|
|
437
|
-
|
|
438
342
|
describe('error handling', () => {
|
|
439
343
|
describe('401 errors', () => {
|
|
440
344
|
it('defaults', async () => {
|
|
@@ -466,15 +370,3 @@ describe('error handling', () => {
|
|
|
466
370
|
}
|
|
467
371
|
})
|
|
468
372
|
})
|
|
469
|
-
|
|
470
|
-
const requestHeadersShouldContain = ({
|
|
471
|
-
headers,
|
|
472
|
-
key,
|
|
473
|
-
value,
|
|
474
|
-
}: {
|
|
475
|
-
headers: Record<string, unknown>
|
|
476
|
-
key: string
|
|
477
|
-
value: unknown
|
|
478
|
-
}) => {
|
|
479
|
-
expect(headers[key]).toContain(value)
|
|
480
|
-
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { OAuthToken } from '
|
|
2
|
-
import { Session } from '
|
|
1
|
+
import { OAuthToken } from '../../types'
|
|
2
|
+
import { Session } from '../../utils/session'
|
|
3
3
|
|
|
4
4
|
const token: OAuthToken = {
|
|
5
5
|
access_token: 'access_token',
|
|
6
6
|
refresh_token: 'refresh_token',
|
|
7
|
-
token_type: undefined,
|
|
7
|
+
token_type: 'undefined',
|
|
8
8
|
created_at: 0,
|
|
9
|
-
expires_in:
|
|
9
|
+
expires_in: 0,
|
|
10
10
|
scope: '',
|
|
11
11
|
}
|
|
12
12
|
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { Uri } from '../../utils'
|
|
2
|
+
|
|
3
|
+
describe('URI', () => {
|
|
4
|
+
it('should be defined', () => {
|
|
5
|
+
expect(Uri).toBeDefined()
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
it('should create a new instance', () => {
|
|
9
|
+
const uri = new Uri({
|
|
10
|
+
session: {
|
|
11
|
+
env: 'production',
|
|
12
|
+
token: undefined,
|
|
13
|
+
type: 'OAuth',
|
|
14
|
+
isAuthenticated: false,
|
|
15
|
+
isChurchCenterToken: false,
|
|
16
|
+
},
|
|
17
|
+
})
|
|
18
|
+
expect(uri).toBeDefined()
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
describe('baseUrl', () => {
|
|
22
|
+
describe('planning center', () => {
|
|
23
|
+
it('should return the correct base URL for production', () => {
|
|
24
|
+
const uri = new Uri({
|
|
25
|
+
session: {
|
|
26
|
+
env: 'production',
|
|
27
|
+
token: undefined,
|
|
28
|
+
type: 'OAuth',
|
|
29
|
+
isAuthenticated: false,
|
|
30
|
+
isChurchCenterToken: false,
|
|
31
|
+
},
|
|
32
|
+
})
|
|
33
|
+
expect(uri.baseUrl).toBe('https://api.planningcenteronline.com')
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('should return the correct base URL for staging', () => {
|
|
37
|
+
const uri = new Uri({
|
|
38
|
+
session: {
|
|
39
|
+
env: 'staging',
|
|
40
|
+
token: undefined,
|
|
41
|
+
type: 'OAuth',
|
|
42
|
+
isAuthenticated: false,
|
|
43
|
+
isChurchCenterToken: false,
|
|
44
|
+
},
|
|
45
|
+
})
|
|
46
|
+
expect(uri.baseUrl).toBe('https://api-staging.planningcenteronline.com')
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('should return the correct base URL for development', () => {
|
|
50
|
+
const uri = new Uri({
|
|
51
|
+
session: {
|
|
52
|
+
env: 'development',
|
|
53
|
+
token: undefined,
|
|
54
|
+
type: 'OAuth',
|
|
55
|
+
isAuthenticated: false,
|
|
56
|
+
isChurchCenterToken: false,
|
|
57
|
+
},
|
|
58
|
+
})
|
|
59
|
+
expect(uri.baseUrl).toBe('http://api.pco.test')
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
describe('church center', () => {
|
|
64
|
+
it('should return the correct base URL for production', () => {
|
|
65
|
+
const uri = new Uri({
|
|
66
|
+
session: {
|
|
67
|
+
env: 'production',
|
|
68
|
+
token: undefined,
|
|
69
|
+
type: 'ChurchCenterOauth',
|
|
70
|
+
isAuthenticated: false,
|
|
71
|
+
isChurchCenterToken: true,
|
|
72
|
+
},
|
|
73
|
+
graph: 'churchcenter',
|
|
74
|
+
})
|
|
75
|
+
expect(uri.baseUrl).toBe('https://api.churchcenter.com')
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('should return the correct base URL for staging', () => {
|
|
79
|
+
const uri = new Uri({
|
|
80
|
+
session: {
|
|
81
|
+
env: 'staging',
|
|
82
|
+
token: undefined,
|
|
83
|
+
type: 'ChurchCenterOauth',
|
|
84
|
+
isAuthenticated: false,
|
|
85
|
+
isChurchCenterToken: true,
|
|
86
|
+
},
|
|
87
|
+
graph: 'churchcenter',
|
|
88
|
+
})
|
|
89
|
+
expect(uri.baseUrl).toBe('https://api.staging.churchcenter.com')
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('should return the correct base URL for development', () => {
|
|
93
|
+
const uri = new Uri({
|
|
94
|
+
session: {
|
|
95
|
+
env: 'development',
|
|
96
|
+
token: undefined,
|
|
97
|
+
type: 'ChurchCenterOauth',
|
|
98
|
+
isAuthenticated: false,
|
|
99
|
+
isChurchCenterToken: true,
|
|
100
|
+
},
|
|
101
|
+
graph: 'churchcenter',
|
|
102
|
+
})
|
|
103
|
+
expect(uri.baseUrl).toBe('http://api.churchcenter.test')
|
|
104
|
+
})
|
|
105
|
+
})
|
|
106
|
+
})
|
|
107
|
+
})
|
|
@@ -17,7 +17,7 @@ const defaultQueryFn = ({ queryKey }: { queryKey: QueryKey }) => {
|
|
|
17
17
|
return apiClient[app].get({ url, data, headers })
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export const
|
|
20
|
+
export const chatQueryClient = new QueryClient({
|
|
21
21
|
defaultOptions: {
|
|
22
22
|
queries: {
|
|
23
23
|
queryFn: defaultQueryFn,
|
|
@@ -35,10 +35,10 @@ export function ApiProvider({ children }: ViewProps) {
|
|
|
35
35
|
useEffect(() => {
|
|
36
36
|
if (!sessionChanged) return
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
chatQueryClient.clear()
|
|
39
39
|
}, [sessionChanged])
|
|
40
40
|
|
|
41
|
-
return <QueryClientProvider client={
|
|
41
|
+
return <QueryClientProvider client={chatQueryClient}>{children}</QueryClientProvider>
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
function useSessionChanged(value: Pick<ChatContextValue, 'token' | 'env'>): boolean {
|
|
@@ -6,12 +6,12 @@ import { ENV, PartialToken, ResponseError, Session } from '../utils'
|
|
|
6
6
|
import { ChatTheme, defaultTheme, DefaultTheme } from '../utils/theme'
|
|
7
7
|
|
|
8
8
|
export type ChatContextValue = {
|
|
9
|
-
token?: PartialToken
|
|
10
|
-
onUnauthorizedResponse: (_response: ResponseError) => void
|
|
11
|
-
theme: ChatTheme
|
|
12
9
|
env?: ENV
|
|
13
|
-
session: Session
|
|
14
10
|
giphyApiKey?: string
|
|
11
|
+
onUnauthorizedResponse: (_response: ResponseError) => void
|
|
12
|
+
session: Session
|
|
13
|
+
theme: ChatTheme
|
|
14
|
+
token?: PartialToken
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
export interface ChatProviderProps extends Omit<ChatContextValue, 'client' | 'theme' | 'session'> {
|
|
@@ -19,12 +19,12 @@ export interface ChatProviderProps extends Omit<ChatContextValue, 'client' | 'th
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
export const ChatContext = createContext<ChatContextValue>({
|
|
22
|
-
theme: defaultTheme('light'),
|
|
23
|
-
token: undefined,
|
|
24
22
|
env: undefined,
|
|
23
|
+
giphyApiKey: undefined,
|
|
25
24
|
onUnauthorizedResponse: () => {},
|
|
26
25
|
session: new Session(),
|
|
27
|
-
|
|
26
|
+
theme: defaultTheme('light'),
|
|
27
|
+
token: undefined,
|
|
28
28
|
})
|
|
29
29
|
|
|
30
30
|
export function ChatProvider({ children, value }: { children: any; value: ChatProviderProps }) {
|
package/src/hooks/use_api.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
import { ApiCollection, ApiResource, ResourceObject } from '../types'
|
|
8
8
|
import { GetRequest, RequestData } from '../utils/client/types'
|
|
9
9
|
import { App, useApiClient } from './use_api_client'
|
|
10
|
-
import { getRequestQueryKey } from './use_suspense_api'
|
|
10
|
+
import { getRequestQueryKey, RequestQueryKey } from './use_suspense_api'
|
|
11
11
|
|
|
12
12
|
interface ApiGetOptions extends GetRequest {
|
|
13
13
|
app?: App
|
|
@@ -16,9 +16,15 @@ interface ApiGetOptions extends GetRequest {
|
|
|
16
16
|
|
|
17
17
|
export const useApiGet = <T extends ResourceObject | ResourceObject[]>(args: ApiGetOptions) => {
|
|
18
18
|
type Resource = ApiResource<T>
|
|
19
|
+
const apiClient = useApiClient()
|
|
19
20
|
|
|
20
21
|
const { data, ...query } = useQuery<Resource, Response>({
|
|
21
22
|
queryKey: getRequestQueryKey(args),
|
|
23
|
+
queryFn: ({ queryKey }) => {
|
|
24
|
+
const [url, d, headers, app = 'chat'] = queryKey as RequestQueryKey
|
|
25
|
+
|
|
26
|
+
return apiClient[app].get({ url, data: d, headers }) as Promise<Resource>
|
|
27
|
+
},
|
|
22
28
|
enabled: args.enabled,
|
|
23
29
|
})
|
|
24
30
|
|
|
@@ -56,7 +62,7 @@ export const useApiPaginator = <T extends ResourceObject>(
|
|
|
56
62
|
const offset = pageParam?.offset || args.data.offset
|
|
57
63
|
const data = { ...args.data, where, offset }
|
|
58
64
|
|
|
59
|
-
return apiClient[app].get({
|
|
65
|
+
return apiClient[app].get<ApiCollection<T>>({
|
|
60
66
|
url: args.url,
|
|
61
67
|
data,
|
|
62
68
|
})
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useContext, useMemo } from 'react'
|
|
2
2
|
import { ChatContext } from '../contexts/chat_context'
|
|
3
3
|
import { Client } from '../utils/client'
|
|
4
|
+
import { Uri } from '../utils'
|
|
4
5
|
|
|
5
6
|
export type App = 'chat' | 'groups' | 'services'
|
|
6
7
|
const apps: App[] = ['chat', 'groups', 'services']
|
|
@@ -9,18 +10,21 @@ export type ApiClient = { [_K in App]: Client }
|
|
|
9
10
|
|
|
10
11
|
export const useApiClient = () => {
|
|
11
12
|
const { session, onUnauthorizedResponse } = useContext(ChatContext)
|
|
13
|
+
|
|
14
|
+
const uri = useMemo(() => new Uri({ session }), [session])
|
|
15
|
+
|
|
12
16
|
const api = useMemo(
|
|
13
17
|
() =>
|
|
14
18
|
apps.reduce((acc, app) => {
|
|
15
19
|
acc[app] = new Client({
|
|
16
|
-
app,
|
|
17
|
-
|
|
20
|
+
root: uri.api(`/${app}/v2`),
|
|
21
|
+
defaultHeaders: uri.headers,
|
|
18
22
|
version: '2018-11-01',
|
|
19
23
|
onUnauthorizedResponse,
|
|
20
24
|
})
|
|
21
25
|
return acc
|
|
22
26
|
}, {} as ApiClient),
|
|
23
|
-
[
|
|
27
|
+
[uri, onUnauthorizedResponse]
|
|
24
28
|
)
|
|
25
29
|
|
|
26
30
|
return api
|