Files
daddy/client/src/util/daddy.ts
William Petit 1120474ad9 Utilisation d'un serveur Go custom pour le backend au lieu de
super-graph

Malheureusement, super-graph n'a pas tenu les promesses qu'il semblait
annoncer.

Je propose donc de basculer sur un serveur Go classique (via goweb).
L'authentification OpenID Connect étant gérée côté backend et non plus
côté frontend.
2020-07-12 19:14:46 +02:00

43 lines
885 B
TypeScript

import { GraphQLClient } from 'graphql-request'
import { Config } from "../config";
export class UnauthorizedError extends Error {
constructor(...args: any[]) {
super(...args)
Object.setPrototypeOf(this, UnauthorizedError.prototype);
}
}
export class DaddyClient {
gql: GraphQLClient
constructor(endpoint: string) {
this.gql = new GraphQLClient(endpoint, {
headers: {
mode: 'cors',
}
});
}
fetchUser(email: string) {
return this.gql.rawRequest(`
query fetchUser {
user(where: {email: {eq: $email}}) {
id
created_at
updated_at
email,
full_name
}
}
`, { email })
.then(this.assertAuthorization)
}
assertAuthorization({ status, data }: any) {
if (status === 401) return Promise.reject(new UnauthorizedError());
return data;
}
}