Skip to content

Commit 880ef16

Browse files
committed
Fix some null-check issues.
1 parent e685599 commit 880ef16

File tree

5 files changed

+37
-35
lines changed

5 files changed

+37
-35
lines changed

packages/powersync-sdk-common/src/client/connection/PowerSyncBackendConnector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { PowerSyncCredentials } from './PowerSyncCredentials';
22
import type { AbstractPowerSyncDatabase } from '../AbstractPowerSyncDatabase';
33

44
export interface PowerSyncBackendConnector {
5-
/** Allows the PowerSync client to retrieve an authentication token from your backend
5+
/** Allows the PowerSync client to retrieve an authentication token from your backend
66
* which is used to authenticate against the PowerSync service.
77
*
88
* This should always fetch a fresh set of credentials - don't use cached
@@ -13,7 +13,7 @@ export interface PowerSyncBackendConnector {
1313
*
1414
* This token is kept for the duration of a sync connection.
1515
*/
16-
fetchCredentials: () => Promise<PowerSyncCredentials>;
16+
fetchCredentials: () => Promise<PowerSyncCredentials | null>;
1717

1818
/** Upload local changes to the app backend.
1919
*

packages/powersync-sdk-common/src/client/sync/stream/AbstractRemote.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ import Logger, { ILogger } from 'js-logger';
22
import { PowerSyncCredentials } from '../../connection/PowerSyncCredentials';
33

44
export type RemoteConnector = {
5-
fetchCredentials: () => Promise<PowerSyncCredentials>;
5+
fetchCredentials: () => Promise<PowerSyncCredentials | null>;
66
};
77

8-
//Refresh at least 30 sec before it expires
8+
// Refresh at least 30 sec before it expires
99
const REFRESH_CREDENTIALS_SAFETY_PERIOD_MS = 30_000;
1010

1111
export const DEFAULT_REMOTE_LOGGER = Logger.get('PowerSyncRemote');
1212

1313
export abstract class AbstractRemote {
14-
protected credentials?: PowerSyncCredentials;
14+
protected credentials: PowerSyncCredentials | null = null;
1515

1616
constructor(
1717
protected connector: RemoteConnector,
1818
protected logger: ILogger = DEFAULT_REMOTE_LOGGER
1919
) {}
2020

21-
async getCredentials(): Promise<PowerSyncCredentials> {
21+
async getCredentials(): Promise<PowerSyncCredentials | null> {
2222
const { expiresAt } = this.credentials ?? {};
2323
if (expiresAt && expiresAt > new Date(new Date().valueOf() + REFRESH_CREDENTIALS_SAFETY_PERIOD_MS)) {
2424
return this.credentials!;
@@ -27,22 +27,22 @@ export abstract class AbstractRemote {
2727
return this.credentials;
2828
}
2929

30-
async getToken() {
31-
const { token } = await this.getCredentials();
32-
if (!token) {
30+
protected async buildRequest(path: string) {
31+
const credentials = await this.getCredentials();
32+
if (credentials != null && (credentials.endpoint == null || credentials.endpoint == '')) {
33+
throw new Error('PowerSync endpoint not configured');
34+
} else if (credentials?.token == null || credentials?.token == '') {
3335
const error: any = new Error(`Not signed in`);
3436
error.status = 401;
3537
throw error;
3638
}
3739

38-
return token;
39-
}
40-
41-
async getHeaders() {
42-
const credentials = await this.getCredentials();
4340
return {
44-
'content-type': 'application/json',
45-
Authorization: `Token ${credentials.token}}`
41+
url: credentials.endpoint + path,
42+
headers: {
43+
'content-type': 'application/json',
44+
Authorization: `Token ${credentials.token}`
45+
}
4646
};
4747
}
4848

packages/powersync-sdk-react-native/src/sync/stream/ReactNativeRemote.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ export const STREAMING_POST_TIMEOUT_MS = 30_000;
55

66
export class ReactNativeRemote extends AbstractRemote {
77
async post(path: string, data: any, headers: Record<string, string> = {}): Promise<any> {
8-
const credentials = await this.getCredentials();
9-
const res = await fetch(credentials.endpoint + path, {
8+
const request = await this.buildRequest(path);
9+
const res = await fetch(request.url, {
1010
method: 'POST',
1111
headers: {
1212
...headers,
13-
...(await this.getHeaders())
13+
...request.headers
1414
},
1515
body: JSON.stringify(data)
1616
});
@@ -23,13 +23,13 @@ export class ReactNativeRemote extends AbstractRemote {
2323
}
2424

2525
async get(path: string, headers?: Record<string, string>): Promise<any> {
26-
const credentials = await this.getCredentials();
26+
const request = await this.buildRequest(path);
2727

28-
const res = await fetch(credentials.endpoint + path, {
28+
const res = await fetch(request.url, {
2929
method: 'GET',
3030
headers: {
3131
...headers,
32-
...(await this.getHeaders())
32+
...request.headers
3333
}
3434
});
3535

@@ -59,7 +59,7 @@ export class ReactNativeRemote extends AbstractRemote {
5959
throw new Error(errorMessage);
6060
}
6161

62-
const credentials = await this.getCredentials();
62+
const request = await this.buildRequest(path);
6363

6464
const timeout =
6565
Platform.OS == 'android'
@@ -72,9 +72,9 @@ export class ReactNativeRemote extends AbstractRemote {
7272
}, STREAMING_POST_TIMEOUT_MS)
7373
: null;
7474

75-
const res = await fetch(credentials.endpoint + path, {
75+
const res = await fetch(request.url, {
7676
method: 'POST',
77-
headers: { ...headers, ...(await this.getHeaders()) },
77+
headers: { ...headers, ...request.headers },
7878
body: JSON.stringify(data),
7979
signal,
8080
cache: 'no-store',

packages/powersync-sdk-web/src/db/sync/WebRemote.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import { AbstractRemote } from '@journeyapps/powersync-sdk-common';
22

33
export class WebRemote extends AbstractRemote {
44
async post(path: string, data: any, headers: Record<string, string> = {}): Promise<any> {
5-
const credentials = await this.getCredentials();
6-
const res = await fetch(credentials.endpoint + path, {
5+
const request = await this.buildRequest(path);
6+
7+
const res = await fetch(request.url, {
78
method: 'POST',
89
headers: {
910
...headers,
10-
...(await this.getHeaders())
11+
...request.headers
1112
},
1213
body: JSON.stringify(data)
1314
});
@@ -20,13 +21,13 @@ export class WebRemote extends AbstractRemote {
2021
}
2122

2223
async get(path: string, headers?: Record<string, string>): Promise<any> {
23-
const credentials = await this.getCredentials();
24+
const request = await this.buildRequest(path);
2425

25-
const res = await fetch(credentials.endpoint + path, {
26+
const res = await fetch(request.url, {
2627
method: 'GET',
2728
headers: {
2829
...headers,
29-
...(await this.getHeaders())
30+
...request.headers
3031
}
3132
});
3233

@@ -43,11 +44,11 @@ export class WebRemote extends AbstractRemote {
4344
headers: Record<string, string> = {},
4445
signal?: AbortSignal
4546
): Promise<any> {
46-
const credentials = await this.getCredentials();
47+
const request = await this.buildRequest(path);
4748

48-
const res = await fetch(credentials.endpoint + path, {
49+
const res = await fetch(request.url, {
4950
method: 'POST',
50-
headers: { ...headers, ...(await this.getHeaders()) },
51+
headers: { ...headers, ...request.headers },
5152
body: JSON.stringify(data),
5253
signal,
5354
cache: 'no-store'

packages/powersync-sdk-web/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
"target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
1919
"strictNullChecks": true
2020
},
21-
"include": ["src/**/*", "tests/**/*"]
21+
"include": ["src/**/*", "tests/**/*"],
22+
"references": [{ "path": "../powersync-sdk-common" }]
2223
}

0 commit comments

Comments
 (0)