node-apple-weatherkit
Version:
Apple WeatherKit API node.js implementation
45 lines • 1.28 kB
JavaScript
/*
* Copyright (c) 2022 RainMachine - Green Electronics LLC
* Author: Nicu Pavel
* License: MIT
*/
import jwt from 'jsonwebtoken';
import { WeatherEndpoint, AvailabilityEndpoint } from './api/endpoints';
export class WeatherKit {
constructor(options) {
this.options = options;
this.token = this.encodeJWT(options);
}
get jwt() {
return this.token;
}
get availability() {
return new AvailabilityEndpoint(this.token);
}
get weather() {
return new WeatherEndpoint(this.token);
}
encodeJWT(options) {
const now = new Date();
const currentTime = (now.getTime() / 1000) >> 0;
if (!options.expireTime) {
options.expireTime = now.setFullYear(now.getFullYear() + 1);
}
const payload = {
iss: options.teamId,
iat: currentTime,
exp: options.expireTime,
sub: options.serviceId,
};
const headers = {
alg: 'ES256',
kid: options.keyId,
id: options.teamId + '.' + options.serviceId,
};
return jwt.sign(payload, options.key, {
algorithm: 'ES256',
header: { ...headers },
});
}
}
//# sourceMappingURL=WeatherKit.js.map