Skip to content

Commit

Permalink
add oauth helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi committed Jan 10, 2024
1 parent 79a9b88 commit df8a7e3
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 20 deletions.
7 changes: 7 additions & 0 deletions .fernignore
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
# Specify files that shouldn't be modified by Fern

README.md

# Oauth Helpers
src/index.ts
src/oauth.ts
src/wrapper
43 changes: 24 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,12 @@ for more details.
```javascript
import { WebflowClient } from "webflow-api";

const authorizeUrlParams = {
const authorizeUrl = WebflowClient.authorizeURL({
state: "your_state",
scope: "sites:read",
client_id: "your_client_id",
redirect_uri: "your_redirect_uri",
response_type: "code",
};

const authorizeUrl = webflowClient.authorizeUrl(authorizeUrlParams); // not yet implemented
clientId: "your_client_id",
redirctUri: "your_redirect_uri",
});

console.log(authorizeUrl);
```
Expand All @@ -71,11 +68,11 @@ Use the `getAccessToken` function and pass in your `client_id`,
```javascript
import { WebflowClient } from "webflow-api";

const accessToken = WebflowClient.getAccessToken(
(client_id = "YOUR_CLIENT_ID"),
(client_secret = "YOUR_CLIENT_SECRET"),
(code = "YOUR_AUTHORIZATION_CODE")
); // not yet implemented
const accessToken = WebflowClient.getAccessToken({
clientId: "your_client_id",
clientSecret: "your_client_secret",
code: "your_authorization_code"
});
```

### Step 3: Instantiate the client
Expand All @@ -90,7 +87,7 @@ const webflow = WebflowClient({ accessToken });

## Webflow Types

All of the types are nested within the `Webflow` object. Let IntelliSense
All of the types are nested within the `Webflow` namespace. Let IntelliSense
guide you!

## Exception Handling
Expand Down Expand Up @@ -123,12 +120,20 @@ By default, requests time out after 60 seconds. You can configure the timeout an
```javascript
import { WebflowClient } from 'webflow';

const webflow = new WebflowClient({
accessToken: 'your_access_token',
requestOptions: {
timeoutInSeconds: 10,
maxRetries: 3,
}
const sites = await webflow.sites.get(..., {
timeoutInSeconds: 30 // override the timeout
});
```

### Retries
The SDK will automatically retry failures with exponential backoff.
You can configure the retries by setting maxRetries.

```javascript
import { WebflowClient } from 'webflow';

const sites = await webflow.sites.get(..., {
maxRetries: 10 // override the reries
});
```

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * as Webflow from "./api";
export { WebflowClient } from "./Client";
export { WebflowClient } from "./wrapper/WebflowClient";
export { WebflowEnvironment } from "./environments";
export { WebflowError, WebflowTimeoutError } from "./errors";
96 changes: 96 additions & 0 deletions src/wrapper/WebflowClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { WebflowClient as FernClient } from "../Client";
import { OauthScope } from "../api/types";
import * as errors from "../errors";
import * as core from "../core";

export class WebflowClient extends FernClient {
/**
* @param clientId The OAuth client ID
* @param state The state
* @param redirectUri The redirect URI
* @param scope The OAuth scopes
* @returns the URL to authorize a user
*/
static authorizeURL({
clientId,
state,
redirectUri,
scope,
}: {
clientId: string;
state?: string;
redirectUri?: string;
scope: OauthScope | OauthScope[];
}): string {
const params: Record<string, string> = { response_type: "code", client_id: clientId };
if (redirectUri != null) {
params["redirect_uri"] = redirectUri;
}
if (state != null) {
params["state"] = state;
}
if (typeof scope === "string") {
params["scope"] = scope;
} else {
params["scope"] = scope.join("+");
}
return `https://webflow.com/oauth/authorize?${qs.stringify(params)}`;

Check failure on line 37 in src/wrapper/WebflowClient.ts

View workflow job for this annotation

GitHub Actions / compile

'qs' refers to a UMD global, but the current file is a module. Consider adding an import instead.
}

/**
* @param clientId The OAuth client ID
* @param clientSecret The OAuth client secret
* @param code The OAuth code
* @param redirect_uri The redirect uri
* @returns access token that can be used to hit our API
*/
static async getAccessToken({
clientId,
clientSecret,
code,
redirectUri,
}: {
clientId: string;
clientSecret: string;
code: string;
redirectUri?: string;
}): Promise<string> {
const body: Record<string, string> = {
client_id: clientId,
client_secret: clientSecret,
code,
grant_type: "authorization_code",
};
if (redirectUri != null) {
body["redirect_uri"] = redirectUri;
}
const response = await core.fetcher({
url: "https://api.webflow.com/oauth/access_token",
method: "POST",
contentType: "application/json",
body,
});
if (response.ok) {
return (response as any)["access_token"];
}

switch (response.error.reason) {
case "status-code":
throw new errors.WebflowError({
statusCode: response.error.statusCode,
body: response.error.body,
});
case "non-json":
throw new errors.WebflowError({
statusCode: response.error.statusCode,
body: response.error.rawBody,
});
case "timeout":
throw new errors.WebflowTimeoutError();
case "unknown":
throw new errors.WebflowError({
message: response.error.errorMessage,
});
}
}
}

0 comments on commit df8a7e3

Please sign in to comment.