feat: Adds user validation via api

Validates user and if specified device

Signed-off-by: Alix von Schirp <github@avonschirp.bootmedia.de>
This commit is contained in:
2025-04-15 15:24:12 +02:00
parent 6a8cf528e3
commit 52b11eeffb

View File

@@ -87,12 +87,20 @@ const MessageSchema = z
export type PushoverMessage = z.infer<typeof MessageSchema>; export type PushoverMessage = z.infer<typeof MessageSchema>;
export interface PushoverResponse { interface PushoverResponse {
status: number; status: number;
}
export interface PushoverMessageResponse extends PushoverResponse {
request: string; request: string;
errors?: string[]; errors?: string[];
} }
export interface PushoverValidationResponse extends PushoverResponse {
devices?: string[];
licenses?: string[];
}
export interface PushoverConfig { export interface PushoverConfig {
token: string; token: string;
defaultUser?: string; defaultUser?: string;
@@ -104,10 +112,15 @@ export interface SendOptions {
verbose?: boolean; verbose?: boolean;
} }
export interface ValidateOptions {
user?: string;
deviceName?: string;
}
export class Pushover { export class Pushover {
private token: string; private token: string;
private defaultUser?: string; private defaultUser?: string;
private apiUrl = "https://api.pushover.net/1/messages.json"; private apiUrl = "https://api.pushover.net/1/";
constructor(config: PushoverConfig) { constructor(config: PushoverConfig) {
this.token = config.token; this.token = config.token;
@@ -210,10 +223,13 @@ export class Pushover {
} }
} }
return this.makeRequest(params); return this.makeRequest("messages.json", params);
} }
private makeRequest(params: URLSearchParams): Promise<PushoverResponse> { private makeRequest(
url: string,
params: URLSearchParams,
): Promise<PushoverResponse> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const options = { const options = {
method: "POST", method: "POST",
@@ -222,7 +238,7 @@ export class Pushover {
}, },
}; };
const req = https.request(this.apiUrl, options, (res) => { const req = https.request(this.apiUrl + url, options, (res) => {
let data = ""; let data = "";
res.on("data", (chunk) => { res.on("data", (chunk) => {
@@ -232,7 +248,11 @@ export class Pushover {
res.on("end", () => { res.on("end", () => {
try { try {
const response = JSON.parse(data) as PushoverResponse; const response = JSON.parse(data) as PushoverResponse;
resolve(response); if (url === "messages.json")
resolve(response as PushoverMessageResponse);
else if (url == "users/validate.json")
resolve(response as PushoverValidationResponse);
else resolve(response);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
reject(new Error(`Failed to parse response: ${data}`)); reject(new Error(`Failed to parse response: ${data}`));
@@ -248,4 +268,19 @@ export class Pushover {
req.end(); req.end();
}); });
} }
/**
* Validate a user and device.
*
* If only user is provided, it will validate the user and return.
*/
validate(options: ValidateOptions): Promise<PushoverValidationResponse> {
const params = new URLSearchParams();
params.append("token", this.token);
params.append("user", options.user ?? this.defaultUser ?? "");
if (options.deviceName) params.append("device", options.deviceName);
return this.makeRequest("users/validate.json", params);
}
} }