Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realm Web: Refresh access token #3020

Merged
merged 9 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 40 additions & 40 deletions packages/realm-network-transport/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/realm-network-transport/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "realm-network-transport",
"version": "0.4.0",
"version": "0.6.0",
"description": "Implements cross-platform fetching used by Realm JS",
"main": "dist/bundle.cjs.js",
"module": "dist/bundle.es.js",
Expand Down
18 changes: 16 additions & 2 deletions packages/realm-network-transport/src/MongoDBRealmError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,39 @@
//
////////////////////////////////////////////////////////////////////////////

import { Method } from "./types";

/**
* TODO: Determine if the shape of an error response is specific to each service or widely used
*/

export class MongoDBRealmError extends Error {
public readonly method: Method;
public readonly url: string;
public readonly statusCode: number;
public readonly statusText: string;
public readonly errorCode: string | undefined;
public readonly link: string | undefined;

constructor(statusCode: number, statusText: string, response: any) {
constructor(
method: Method,
url: string,
statusCode: number,
statusText: string,
response: any,
) {
if (
typeof response === "object" &&
typeof response.error === "string"
) {
const statusSummary = statusText
? `status ${statusCode} ${statusText}`
: `status ${statusCode}`;
super(`${response.error} (${statusSummary})`);
super(
`Request failed (${method} ${url}): ${response.error} (${statusSummary})`,
);
this.method = method;
this.url = url;
this.statusText = statusText;
this.statusCode = statusCode;
this.errorCode = response.error_code;
Expand Down
50 changes: 10 additions & 40 deletions packages/realm-network-transport/src/NetworkTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,13 @@
////////////////////////////////////////////////////////////////////////////

import { MongoDBRealmError } from "./MongoDBRealmError";
import { NetworkTransport, Request, ResponseHandler, Headers } from "./types";

declare const process: any;
declare const require: ((id: string) => any) | undefined;

const isNodeProcess = typeof process === "object";

export type Method = "GET" | "POST" | "DELETE" | "PUT";

export type Headers = { [name: string]: string };

export interface Request<RequestBody> {
method: Method;
url: string;
timeoutMs?: number;
headers?: Headers;
body?: RequestBody | string;
}

export interface Response {
statusCode: number;
headers: Headers;
body: string;
}

export type SuccessCallback = (response: Response) => void;

export type ErrorCallback = (err: Error) => void;

export interface ResponseHandler {
onSuccess: SuccessCallback;
onError: ErrorCallback;
}

export interface NetworkTransport {
fetchAndParse<RequestBody extends any, ResponseBody extends any>(
request: Request<RequestBody>,
): Promise<ResponseBody>;
fetchWithCallbacks<RequestBody extends any>(
request: Request<RequestBody>,
handler: ResponseHandler,
): void;
}

export class DefaultNetworkTransport implements NetworkTransport {
public static fetch: typeof fetch;
public static AbortController: typeof AbortController;
Expand Down Expand Up @@ -128,6 +92,8 @@ export class DefaultNetworkTransport implements NetworkTransport {
contentType.startsWith("application/json")
) {
throw new MongoDBRealmError(
request.method,
request.url,
response.status,
response.statusText,
await response.json(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(void comment) Is this actually awaited before thrown? I'm more & more impressed with TS.

Copy link
Member Author

@kraenhansen kraenhansen Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is what awaited? (or is your "void comment" because I forced pushed a change? 😺 )

Expand All @@ -138,9 +104,13 @@ export class DefaultNetworkTransport implements NetworkTransport {
);
}
} catch (err) {
throw new Error(
`Request failed (${request.method} ${request.url}): ${err.message}`,
);
if (err instanceof MongoDBRealmError) {
throw err;
} else {
throw new Error(
`Request failed (${request.method} ${request.url}): ${err.message}`,
);
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/realm-network-transport/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
////////////////////////////////////////////////////////////////////////////

export {
DefaultNetworkTransport,
NetworkTransport,
Request,
Response,
Method,
SuccessCallback,
ErrorCallback,
ResponseHandler,
} from "./NetworkTransport";
} from "./types";
export { DefaultNetworkTransport } from "./NetworkTransport";
export { MongoDBRealmError } from "./MongoDBRealmError";
54 changes: 54 additions & 0 deletions packages/realm-network-transport/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2020 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

export type Method = "GET" | "POST" | "DELETE" | "PUT";

export type Headers = { [name: string]: string };

export interface Request<RequestBody> {
method: Method;
url: string;
timeoutMs?: number;
headers?: Headers;
body?: RequestBody | string;
}

export interface Response {
statusCode: number;
headers: Headers;
body: string;
}

export type SuccessCallback = (response: Response) => void;

export type ErrorCallback = (err: Error) => void;

export interface ResponseHandler {
onSuccess: SuccessCallback;
onError: ErrorCallback;
}

export interface NetworkTransport {
fetchAndParse<RequestBody extends any, ResponseBody extends any>(
request: Request<RequestBody>,
): Promise<ResponseBody>;
fetchWithCallbacks<RequestBody extends any>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering why fetch with parsing is promise-based, and fetch without parsing (or limited parsing) is callback-based?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because when Realm JS is using this, its easier to consume from JS if a handler can be passed when invoked instead of having to call methods on the returned promise. Makes sense?

request: Request<RequestBody>,
handler: ResponseHandler,
): void;
}
Loading