-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
103 lines (94 loc) · 2.86 KB
/
auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { AxiosInstance } from 'axios'
import { enumFromValue } from './enum_from_value.ts'
import { defaultTransformers } from './axios_default_transformers.ts'
import { whoAmIUrl } from './urls.ts'
import type { DateTime, Replace } from './types.ts'
/**
* TODO(@joscha): Enum is most likely incomplete
*/
export enum GrantType {
API_KEY = 'api_key',
}
/**
* TODO(@joscha): Enum is most likely incomplete
*/
export enum Scope {
EXTERNAL_API = 'external_api',
}
export type Tenant = {
/** Tenant ID; unrelated to any entity the current Affinity instance */
id: number
name: string
/** `https://<subdomain>.affinity.co` */
subdomain: string
}
export type User = {
/** User ID; This is also the ID of the person in the current Affinity instance.
* E.g. you can use it to get the person via `https://<subdomain>.affinity.co/persons/<id>`
*/
id: number
firstName: string
lastName: string
/** The primary email of the user */
email: string
}
export type GrantRaw = {
type: string
scope: string
createdAt: DateTime
}
export type WhoAmIResponseRaw = {
/**
* Information about the Affinity instance the user belongs to.
*/
tenant: Tenant
/**
* Data about the user whose API key was used for the endpoint.
*/
user: User
/**
* Data about the type of authentication and metadata about the API key.
*/
grant: GrantRaw
}
export type WhoAmIResponse = Replace<WhoAmIResponseRaw, {
grant: {
type: GrantType
scope: Scope
createdAt: Date
}
}>
/**
* @module
*/
export class Auth {
/** @hidden */
constructor(private readonly axios: AxiosInstance) {
}
/**
* Gets information about the user sending the request, and their affiliate company.
* There are no query or path parameters for this method. The information needed is contained within the API key.
*
* [More information](https://api-docs.affinity.co/#the-whoami-resource)
*/
async whoAmI(): Promise<WhoAmIResponse> {
const response = await this.axios.get<WhoAmIResponse>(whoAmIUrl(), {
transformResponse: [
...defaultTransformers(),
(json: WhoAmIResponseRaw) => {
return {
...json,
grant: {
createdAt: new Date(json.grant.createdAt),
// TODO(@joscha): when the API is complete, throw here on undefined values
type: enumFromValue(json.grant.type, GrantType),
// TODO(@joscha): when the API is complete, throw here on undefined values
scope: enumFromValue(json.grant.scope, Scope),
},
}
},
],
})
return response.data
}
}