hmm.js
· 4.3 KiB · JavaScript
Raw
module.exports = async function(fastify) {
fastify.route({
url: '/fetch/:userId',
method: ['GET'],
config: {
rateLimit: {
max: 100,
timeWindow: 10000,
errorResponseBuilder: function(request, context) {
return {
code: 429,
error: 'Enhance your calm',
message: `Rate limited, please try again in: ${this.timeWindow}ms`,
date: Date.now(),
expiresIn: this.timeWindow
}
},
onExceeding: function(request, context) {
return {
code: 429,
error: 'Enhance your calm',
message: `Rate limited, please try again in: ${this.timeWindow}ms`,
date: Date.now(),
expiresIn: this.timeWindow,
}
},
onExceeded: function (request, context) {
return {
code: 420,
error: 'Enhance your calm',
message: `Whoops, looks like you have been rate limited. Please wait: ${this.timeWindow}ms`,
date: Date.now(),
expiresIn: this.timeWindow
}
}
}
},
schema: {
summary: 'Get a CordX user\'s Discord Profile',
description: 'Returns a given cordx user\'s discord id, global name etc',
tags: ['Users'],
params: {
type: 'object',
properties: {
userId: {
type: 'string',
description: 'A valid discord user id/snowflake'
}
}
},
response: {
200: {
description: 'Successful request',
type: 'object',
properties: {
id: { type: 'string'},
globalName: { type: 'string'},
displayName: { type: 'string'},
userName: { type: 'string'}
}
},
404: {
description: 'User not found',
type: 'object',
properties: {
code: { type: 'string'},
message: { type: 'string'},
status: { type: 'number'},
}
},
429: {
description: 'Rate limited',
type: 'object',
properties: {
code: { type: 'number'},
error: { type: 'string'},
message: { type: 'string'},
date: { type: 'string'},
expiresIn: { type: 'number'}
}
}
}
},
preHandler: async (request, reply) => {
const { userId } = request.params
let cordx_user = await request.client.users.fetch(userId);
if (!cordx_user) return reply.code(404).send({
code: 'USER_NOT_FOUND',
message: `Our discord client searched far and wide but was unable to locate a user with the ID: ${userId}. If needed you can use the #{/v3/users/discord/:userId} route to fetch a user using the discord api!`,
status: 404
});
if (!userId) {
reply.code(404).send({
code: 'USER_NOT_FOUND',
message: `The provided user ${userId} was not found!`,
status: 404
})
return null
}
},
handler: async (request, reply) => {
let user = request.params.userId;
let cordx_user = await request.client.users.fetch(user);
return reply.code(200).send({
id: cordx_user.id,
globalName: cordx_user.globalName,
displayName: cordx_user.displayName,
userName: cordx_user.username
})
}
});
}
1 | module.exports = async function(fastify) { |
2 | |
3 | fastify.route({ |
4 | url: '/fetch/:userId', |
5 | method: ['GET'], |
6 | config: { |
7 | rateLimit: { |
8 | max: 100, |
9 | timeWindow: 10000, |
10 | errorResponseBuilder: function(request, context) { |
11 | return { |
12 | code: 429, |
13 | error: 'Enhance your calm', |
14 | message: `Rate limited, please try again in: ${this.timeWindow}ms`, |
15 | date: Date.now(), |
16 | expiresIn: this.timeWindow |
17 | } |
18 | }, |
19 | onExceeding: function(request, context) { |
20 | return { |
21 | code: 429, |
22 | error: 'Enhance your calm', |
23 | message: `Rate limited, please try again in: ${this.timeWindow}ms`, |
24 | date: Date.now(), |
25 | expiresIn: this.timeWindow, |
26 | } |
27 | }, |
28 | onExceeded: function (request, context) { |
29 | return { |
30 | code: 420, |
31 | error: 'Enhance your calm', |
32 | message: `Whoops, looks like you have been rate limited. Please wait: ${this.timeWindow}ms`, |
33 | date: Date.now(), |
34 | expiresIn: this.timeWindow |
35 | } |
36 | } |
37 | } |
38 | }, |
39 | schema: { |
40 | summary: 'Get a CordX user\'s Discord Profile', |
41 | description: 'Returns a given cordx user\'s discord id, global name etc', |
42 | tags: ['Users'], |
43 | params: { |
44 | type: 'object', |
45 | properties: { |
46 | userId: { |
47 | type: 'string', |
48 | description: 'A valid discord user id/snowflake' |
49 | } |
50 | } |
51 | }, |
52 | response: { |
53 | 200: { |
54 | description: 'Successful request', |
55 | type: 'object', |
56 | properties: { |
57 | id: { type: 'string'}, |
58 | globalName: { type: 'string'}, |
59 | displayName: { type: 'string'}, |
60 | userName: { type: 'string'} |
61 | } |
62 | }, |
63 | 404: { |
64 | description: 'User not found', |
65 | type: 'object', |
66 | properties: { |
67 | code: { type: 'string'}, |
68 | message: { type: 'string'}, |
69 | status: { type: 'number'}, |
70 | } |
71 | }, |
72 | 429: { |
73 | description: 'Rate limited', |
74 | type: 'object', |
75 | properties: { |
76 | code: { type: 'number'}, |
77 | error: { type: 'string'}, |
78 | message: { type: 'string'}, |
79 | date: { type: 'string'}, |
80 | expiresIn: { type: 'number'} |
81 | } |
82 | } |
83 | } |
84 | }, |
85 | preHandler: async (request, reply) => { |
86 | const { userId } = request.params |
87 | let cordx_user = await request.client.users.fetch(userId); |
88 | |
89 | if (!cordx_user) return reply.code(404).send({ |
90 | code: 'USER_NOT_FOUND', |
91 | message: `Our discord client searched far and wide but was unable to locate a user with the ID: ${userId}. If needed you can use the #{/v3/users/discord/:userId} route to fetch a user using the discord api!`, |
92 | status: 404 |
93 | }); |
94 | |
95 | if (!userId) { |
96 | reply.code(404).send({ |
97 | code: 'USER_NOT_FOUND', |
98 | message: `The provided user ${userId} was not found!`, |
99 | status: 404 |
100 | }) |
101 | return null |
102 | } |
103 | }, |
104 | handler: async (request, reply) => { |
105 | |
106 | let user = request.params.userId; |
107 | |
108 | let cordx_user = await request.client.users.fetch(user); |
109 | |
110 | return reply.code(200).send({ |
111 | id: cordx_user.id, |
112 | globalName: cordx_user.globalName, |
113 | displayName: cordx_user.displayName, |
114 | userName: cordx_user.username |
115 | }) |
116 | } |
117 | }); |
118 | } |