Skip to content

Commit

Permalink
add prettier config and formt
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-platform committed Mar 21, 2023
1 parent 52fc0d4 commit afc9883
Show file tree
Hide file tree
Showing 88 changed files with 907 additions and 682 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/lib
/types
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"trailingComma": "none",
"arrowParens": "avoid"
}
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![Build Status](https://travis-ci.org/platformsh/platformsh-client-js.svg?branch=master)](https://travis-ci.org/platformsh/platformsh-client-js)

# Platform.sh API client

This is a isomorphic Javascript library for accessing the Platform.sh API.
Expand All @@ -7,7 +8,7 @@ We recommend you use the [Platform.sh CLI](https://github.com/platformsh/platfor

## Install

Run ``` npm install platformsh-client ```
Run `npm install platformsh-client`

## Usage

Expand All @@ -29,12 +30,12 @@ client.getProjects().then(projects => {

## Development mode

Run ``` npm run dev ```
Run `npm run dev`

## Build

Run ``` npm run build ```
Run `npm run build`

## Test

Run ``` npm test ```
Run `npm test`
17 changes: 8 additions & 9 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type RequestOptions = {

export type RequestConfiguration = RequestOptions & RequestInit;

let authenticationPromise : Promise<JWTToken>;
let authenticationPromise: Promise<JWTToken>;

export const setAuthenticationPromise = (promise: Promise<JWTToken>) => {
authenticationPromise = promise;
Expand Down Expand Up @@ -39,10 +39,7 @@ const decodeHeaderString = (header: string) => {
.replace("Bearer", "")
.split(",")
.reduce<Record<string, string>>((acc, cu) => {
const [key, value] = cu
.replace(/"/g, "")
.trim()
.split("=");
const [key, value] = cu.replace(/"/g, "").trim().split("=");
acc[key] = value;
return acc;
}, {});
Expand Down Expand Up @@ -85,21 +82,23 @@ export const request = (
};

if (method !== "GET" && method !== "HEAD" && body) {
const d: BodyInit = isFormData(data) ? data as FormData : JSON.stringify(body);
const d: BodyInit = isFormData(data)
? (data as FormData)
: JSON.stringify(body);
requestConfig.body = d;
}

return new Promise((resolve, reject) => {
fetch(apiUrl, requestConfig)
.then(response => {
if (response.status === 401) {
const config: ClientConfiguration = getConfig();
const config: ClientConfiguration = getConfig();
const extra_params = getChallengeExtraParams(response.headers);

// Prevent an endless loop which happens in case of re-authentication with the access token.
// We want to retry only once, trying to renew the token.
if (typeof config.access_token === "undefined" && retryNumber < 2) {
return authenticate({ ...config, extra_params }, true).then((t) => {
return authenticate({ ...config, extra_params }, true).then(t => {
resolve(
authenticatedRequest(
url,
Expand Down Expand Up @@ -159,7 +158,7 @@ export const request = (

export const authenticatedRequest = (
url: string,
method: string = "GET",
method: string = "GET",
data?: FormData | object | undefined,
additionalHeaders: Record<string, string> = {},
retryNumber: number = 0,
Expand Down
48 changes: 34 additions & 14 deletions src/authentication/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ import {
jso_getCodeVerifier,
PKCERequest
} from "../jso";
import { getConfig, ClientConfiguration, DefaultClientConfiguration } from "../config";
import {
getConfig,
ClientConfiguration,
DefaultClientConfiguration
} from "../config";

type IFrameOption = {
sandbox?: string
sandbox?: string;
};

let basicAuth: string;
Expand All @@ -34,8 +38,13 @@ if (isNode) {
basicAuth = btoa("platform-cli:");
}

function createIFrame(src: string, options: IFrameOption = {}): HTMLIFrameElement {
let iframe: HTMLIFrameElement = document.getElementById("logiframe-platformsh") as HTMLIFrameElement;
function createIFrame(
src: string,
options: IFrameOption = {}
): HTMLIFrameElement {
let iframe: HTMLIFrameElement = document.getElementById(
"logiframe-platformsh"
) as HTMLIFrameElement;

if (iframe) {
return iframe;
Expand All @@ -51,8 +60,8 @@ function createIFrame(src: string, options: IFrameOption = {}): HTMLIFrameElemen
iframe.src = src;
document.body.appendChild(iframe);

if(iframe.contentWindow) {
iframe.contentWindow.onerror = function(msg, url, line) {
if (iframe.contentWindow) {
iframe.contentWindow.onerror = function (msg, url, line) {
if (msg === "[IFRAME ERROR MESSAGE]") {
return true;
}
Expand Down Expand Up @@ -148,7 +157,12 @@ const getTokenWithAuthorizationCode = async (
return await resp.json();
};

async function authorizationCodeCallback(config: DefaultClientConfiguration, codeVerifier: string, code: string, state?: string) {
async function authorizationCodeCallback(
config: DefaultClientConfiguration,
codeVerifier: string,
code: string,
state?: string
) {
const atoken = await getTokenWithAuthorizationCode(
config.authentication_url,
config.client_id,
Expand All @@ -167,14 +181,17 @@ async function authorizationCodeCallback(config: DefaultClientConfiguration, cod
return atoken;
}

function logInWithRedirect(reset: boolean = false, extraParams?: Record<string, string>) {
function logInWithRedirect(
reset: boolean = false,
extraParams?: Record<string, string>
) {
console.log("In redirect...");
return new Promise(async (resolve, reject) => {
const config = getConfig();
const auth = {
...config,
response_mode: config.response_mode,
prompt: config.prompt,
prompt: config.prompt
};
let pkce: PKCERequest;

Expand Down Expand Up @@ -209,7 +226,7 @@ function logInWithRedirect(reset: boolean = false, extraParams?: Record<string,

if (oauthResp) {
const codeVerifier = jso_getCodeVerifier(config.provider);
if(codeVerifier && oauthResp.code) {
if (codeVerifier && oauthResp.code) {
return resolve(
await authorizationCodeCallback(
auth,
Expand All @@ -236,14 +253,14 @@ function logInWithRedirect(reset: boolean = false, extraParams?: Record<string,
} catch {}
}

const authUrl = encodeURL(auth.authorization, {...req, ...extraParams});
const authUrl = encodeURL(auth.authorization, { ...req, ...extraParams });

const iframe = createIFrame(authUrl, {
sandbox: "allow-same-origin"
});
let attempt = 0;

const listener = setInterval(async function() {
const listener = setInterval(async function () {
let href;
let iframeDidReturnError;

Expand Down Expand Up @@ -477,7 +494,11 @@ export const logInWithPopUp = async (reset: boolean = false) => {
return jso_getToken(authConfig.provider);
};

export default (token?: string, reset: boolean = false, config?: Partial<ClientConfiguration>) => {
export default (
token?: string,
reset: boolean = false,
config?: Partial<ClientConfiguration>
) => {
if (isNode && token) {
return logInWithToken(token).catch(e => new Error(e));
}
Expand All @@ -496,4 +517,3 @@ export default (token?: string, reset: boolean = false, config?: Partial<ClientC

return logInWithRedirect(reset, config?.extra_params);
};

18 changes: 9 additions & 9 deletions src/authentication/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { ClientConfiguration } from "../config";
let authenticationInProgress: boolean = false;

export type JWTToken = {
access_token: string,
expires: number,
expires_in: number,
token_type: string,
scope: string
access_token: string;
expires: number;
expires_in: number;
token_type: string;
scope: string;
};

export default (
Expand All @@ -26,7 +26,7 @@ export default (
popupMode,
response_mode,
prompt,
extra_params,
extra_params
}: ClientConfiguration,
reset = false
): Promise<JWTToken> => {
Expand All @@ -46,11 +46,11 @@ export default (
popupMode,
response_mode,
prompt,
extra_params,
extra_params
});
}
if(promise) {

if (promise) {
setAuthenticationPromise(promise);

promise.then(() => {
Expand Down
52 changes: 26 additions & 26 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@ const DEFAULT_ACCOUNT_URL = "https://accounts.platform.sh";
const DEFAULT_API_URL = "https://api.platform.sh/api";

export type ClientConfiguration = {
provider?: string,
client_id?: string,
account_url?: string,
api_url: string,
authentication_url?: string,
scope?: Array<string>,
authorization: string,
logout_url?: string,
access_token?: string,
api_token?: string,
popupMode?: boolean,
response_mode?: string,
prompt?: string,
base_url?: string,
redirect_uri?: string,
response_type?: string,
onBeforeRedirect?: (location: string) => void,
provider?: string;
client_id?: string;
account_url?: string;
api_url: string;
authentication_url?: string;
scope?: Array<string>;
authorization: string;
logout_url?: string;
access_token?: string;
api_token?: string;
popupMode?: boolean;
response_mode?: string;
prompt?: string;
base_url?: string;
redirect_uri?: string;
response_type?: string;
onBeforeRedirect?: (location: string) => void;
extra_params?: Record<string, string>;
};

export type DefaultClientConfiguration = ClientConfiguration & {
scope: Array<string>
redirect_uri:string
provider:string
client_id:string
account_url:string
authentication_url:string
prompt:string
response_type:string
}
scope: Array<string>;
redirect_uri: string;
provider: string;
client_id: string;
account_url: string;
authentication_url: string;
prompt: string;
response_type: string;
};

const getConfigDefault = (
baseUrl: string = DEFAULT_ACCOUNT_URL,
Expand Down
Loading

0 comments on commit afc9883

Please sign in to comment.