-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
128 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}`; | ||
} | ||
|
||
/** | ||
* @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, | ||
}); | ||
} | ||
} | ||
} |