-
Notifications
You must be signed in to change notification settings - Fork 0
/
memberCreation.ts
81 lines (69 loc) · 2.26 KB
/
memberCreation.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
import type { FromSchema } from 'json-schema-to-ts';
import { compileSchema } from '../../utils/serialisation/jsonSchema.js';
import { JsonDeserialiser } from '../../utils/serialisation/JsonDeserialiser.js';
import { JsonValue } from '../../utils/serialisation/JsonValue.js';
import type { PostRequest } from '../../utils/http.js';
import { Command } from '../Command.js';
import type { EndpointInput } from '../EndpointInput.js';
import type { MemberRole } from './MemberRole.js';
const RESPONSE_SCHEMA = {
type: 'object',
properties: {
self: { type: 'string' },
publicKeys: { type: 'string' },
publicKeyImportTokens: { type: 'string' },
},
required: ['self', 'publicKeys', 'publicKeyImportTokens'],
} as const;
const RESPONSE_VALIDATOR = compileSchema(RESPONSE_SCHEMA);
/**
* Input to the {@link MemberCreationCommand}.
* @category Member creation
*/
export interface MemberCreationInput extends EndpointInput {
/**
* The name of the member, if they're a user.
*
* Leave `undefined` if the member is a bot.
*/
readonly name?: string;
/**
* The email address of the member, if they should be allowed to access the API.
*/
readonly email?: string;
/**
* The role of the member.
*/
readonly role: MemberRole;
}
/**
* Output to the {@link MemberCreationCommand}.
* @property {string} self Path to newly-created member.
* @property {string} publicKeys Path to newly-created member's public keys.
* @property {string} publicKeyImportTokens Path to newly-created member's public key import tokens.
* @category Member creation
* @interface
*/
export type MemberCreationOutput = FromSchema<typeof RESPONSE_SCHEMA>;
/**
* Command to create a new member.
* @category Member creation
*/
export class MemberCreationCommand extends Command<
MemberCreationInput,
MemberCreationOutput,
MemberCreationOutput
> {
public responseDeserialiser = new JsonDeserialiser(RESPONSE_VALIDATOR);
public override getRequest(): PostRequest {
const body = new JsonValue({
name: this.input.name,
email: this.input.email,
role: this.input.role,
});
return { body, method: 'POST', path: this.input.endpoint };
}
public getOutput(responseBody: MemberCreationOutput): MemberCreationOutput {
return responseBody;
}
}