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

Consider keywords async and await (since TypeScript 1.7) #27

Closed
juergenzimmermann opened this issue Apr 15, 2016 · 5 comments
Closed

Consider keywords async and await (since TypeScript 1.7) #27

juergenzimmermann opened this issue Apr 15, 2016 · 5 comments
Assignees

Comments

@juergenzimmermann
Copy link

When I'm using async and await in a TypeScript file, then clang-format is formatting as follows:

const
body: string = `username=${username}&password=${password}`;
const
request: Request = new Request(...
@mprobst
Copy link
Contributor

mprobst commented Apr 15, 2016

Mh, I don't see async or await in your code example at all? The example you gave formats properly.

@juergenzimmermann
Copy link
Author

I just wanted to show a part of the result of the following file:

import {decodeToken} from './iam_jwt';
import {saveAuthorization, getAuthorizationValue, getRoles, deleteAuthorization} from './iam_cookies';
import {toBase64, BASE_URI, HTTPS, isPresent} from '../shared/shared';

const loginUri: string = `${BASE_URI}login`;

/**
 * @param username {username} als String
 * @param password {password} als String
 * @return void
 */
export async function login(username: string, password: string): Promise<void> {
    'use strict';
    console.log(`iam.login(): username=${username}, password=${password}`);

    if (isMocking()) {
        // Mocking: String fuer Basic-Authentifizierung
        const authorizationValue: string =
            `Basic ${toBase64(username, password)}`;
        console.log(`iam.login(): authorization=${authorizationValue}`);
        const expiration: number = Date.now() + 24 * 60 * 60 * 1000;
        saveAuthorization(authorizationValue, 'admin,mitarbeiter', expiration);
        return;
    }

    console.log(`Login URI = ${loginUri}`);

    // https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
    const headers: Headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');

    // Seltsame Formatierung durch clang-format bei async/await
    // https://github.com/angular/clang-format/issues/27
    const body: string = `username=${username}&password=${password}`;
    const request: Request = new Request(
        loginUri, {method: 'POST', headers: headers, body: body});

    // ES 2015: Promise.then(...) statt async/await in ES 2017
    //   fetch(request)
    //   .then((response: Response): Promise<any> => {...})
    //   .then((json: any) => {...})
    //   .catch((err: any) => {...});

    let response: Response;
    try {
        response = await fetch(request);
    } catch (err) {
        console.error('iam.login: Kommunikationsfehler mit dem Appserver');
        return Promise.reject(
            new Error('Kommunikationsfehler mit dem Appserver'));
    }

    const status: number = response.status;
    console.log(`status=${status}`);
    if (status !== 200) {
        return Promise.reject(new Error(response.statusText));
    }

    const json: any = await response.json();
    console.log('json', json);
    const token: string = json.token;
    const authorizationValue: string = `Bearer ${token}`;
    console.log(`authorizationValue=${authorizationValue}`);

    const rolesValue: string = json.roles.join();
    console.log(`rolesValue=${rolesValue}`);

    const decodedToken: any = decodeToken(token);
    console.log('decodedToken', decodedToken);
    if (!isPresent(decodedToken.exp)) {
        return;
    }

    // Expiration: Sekunden beim Token, Millisekunden beim Cookie
    const expiration: number = decodedToken.exp * 1000;
    console.log(`fetch.then(): exp=${expiration}`);
    saveAuthorization(authorizationValue, rolesValue, expiration);
}

/**
 * @return void
 */
export function logout(): void {
    'use strict';
    console.log('iam.logout()');
    deleteAuthorization();
}

/**
 * @return String fuer JWT oder Basic-Authentifizierung
 */
export function getAuthorization(): string {
    'use strict';
    return getAuthorizationValue();
}

/**
 * @return true, falls ein User eingeloggt ist; sonst false.
 */
export function isLoggedIn(): boolean {
    'use strict';
    return getAuthorization() !== null;
}

/**
 * @return true, falls ein User in der Rolle "admin" eingeloggt ist;
 *         sonst false.
 */
export function isAdmin(): boolean {
    'use strict';
    if (!isLoggedIn()) {
        return false;
    }

    // z.B. 'admin,mitarbeiter'
    const rolesStr: string = getRoles();
    if (rolesStr === null) {
        return false;
    }

    // z.B. ['admin', 'mitarbeiter']
    const rolesArray: Array<string> = rolesStr.split(',');
    return rolesArray !== null
        && rolesArray.find(r => r === 'admin') !== undefined;
}

function isMocking(): boolean {
    'use strict';
    return !BASE_URI.startsWith(HTTPS);
}

@mprobst
Copy link
Contributor

mprobst commented Apr 17, 2016

Minimal repro:

export async function login(username: string): Promise<Void> {
  const body : string = 'x';
}

Should format without the whitespace in body :.

@mprobst
Copy link
Contributor

mprobst commented Apr 18, 2016

Fix at: http://reviews.llvm.org/D19204

@mprobst mprobst self-assigned this Apr 18, 2016
@mprobst
Copy link
Contributor

mprobst commented Apr 24, 2016

This should be fixed with the latest version, please reopen if you still encounter issues.

@mprobst mprobst closed this as completed Apr 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants