cowin-api-wrapper
Version:
API wrapper for COWIN's public API with TypeScript support.
156 lines • 6.42 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDistricts = exports.getStates = exports.findAppointmentsByDistrict = exports.findAppointmentsByPin = void 0;
const superagent_1 = __importDefault(require("superagent"));
const format_response_1 = require("./format-response");
const URLS = {
BASE_URL: "https://cdn-api.co-vin.in/api/v2",
FIND_BY_PIN: "appointment/sessions/public/findByPin",
FIND_BY_DISTRICT: "appointment/sessions/public/findByDistrict",
CALENDAR_BY_PIN: "appointment/sessions/public/calendarByPin",
CALENDAR_BY_DISTRICT: "appointment/sessions/public/calendarByDistrict",
GET_STATES: "admin/location/states",
GET_DISTRICTS: "admin/location/districts",
};
/**
* Find vaccination sessions by PIN.
* By default searches for current date.
* @param { string | number } pin
* @param { Date } date
*/
const findAppointmentsByPinDay = (pin, date = new Date()) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
const url = `${URLS.BASE_URL}/${URLS.FIND_BY_PIN}`;
try {
const response = yield superagent_1.default
.get(url)
.query({ pincode: pin, date: format_response_1.formatDate(date) });
const appointments = response.body.sessions.map(format_response_1.mapSessionResponse);
return { appointments, error: null, isError: false };
}
catch (err) {
const badRequest = (_a = err.response) === null || _a === void 0 ? void 0 : _a.body;
return { appointments: null, error: badRequest, isError: true };
}
});
/**
* Find vaccination sessions by district.
* By default searches for current date
* @param { number } districtId
* @param { Date } date
*/
const findAppointmentsByDistrictDay = (districtId, date = new Date()) => __awaiter(void 0, void 0, void 0, function* () {
var _b;
const url = `${URLS.BASE_URL}/${URLS.FIND_BY_DISTRICT}`;
try {
const response = yield superagent_1.default
.get(url)
.query({ district_id: districtId, date: format_response_1.formatDate(date) });
const appointments = response.body.sessions.map(format_response_1.mapSessionResponse);
return { appointments, error: null, isError: false };
}
catch (err) {
const badRequest = (_b = err.response) === null || _b === void 0 ? void 0 : _b.body;
return { appointments: null, error: badRequest, isError: true };
}
});
/**
* Find vaccine sessions by pin for a week.
* By default week starts from current date
* @param { string | number } pin
* @param { Date } date
*/
const findAppointmentsByPinWeek = (pin, date = new Date()) => __awaiter(void 0, void 0, void 0, function* () {
const url = `${URLS.BASE_URL}/${URLS.CALENDAR_BY_PIN}`;
try {
const response = yield superagent_1.default
.get(url)
.query({ pincode: pin, date: format_response_1.formatDate(date) });
const appointments = response.body.centers;
return { appointments, error: null, isError: false };
}
catch (err) {
return { appointments: null, error: null, isError: false };
}
});
/**
* Find vaccine sessions by district id for a week.
* By default week starts from current date
* @param { number } districtId
* @param { Date } date
*/
const findAppointmentsByDistrictWeek = (districtId, date = new Date()) => __awaiter(void 0, void 0, void 0, function* () {
var _c;
const url = `${URLS.BASE_URL}/${URLS.CALENDAR_BY_DISTRICT}`;
try {
const response = yield superagent_1.default
.get(url)
.query({ district_id: districtId, date: format_response_1.formatDate(date) });
const appointments = response.body.centers;
return { appointments, error: null, isError: false };
}
catch (err) {
const badRequest = (_c = err.response) === null || _c === void 0 ? void 0 : _c.body;
return { appointments: null, error: badRequest, isError: true };
}
});
/**
* Search for appointments using pin.
* @param { string | number } pin
* @param { Options } opts Allows you to switch between per day or per week results
*/
const findAppointmentsByPin = (pin, opts = {}) => {
const { date = new Date(), week = false } = opts;
if (week) {
return findAppointmentsByPinWeek(pin, date);
}
else {
return findAppointmentsByPinDay(pin, date);
}
};
exports.findAppointmentsByPin = findAppointmentsByPin;
/**
* Search for appointments using district.
* @param { string | number } pin
* @param { Options } opts Allows you to switch between per day or per week results
*/
const findAppointmentsByDistrict = (districtId, opts = {}) => {
const { date = new Date(), week = false } = opts;
if (week) {
return findAppointmentsByDistrictWeek(districtId, date);
}
else {
return findAppointmentsByDistrictDay(districtId, date);
}
};
exports.findAppointmentsByDistrict = findAppointmentsByDistrict;
/**
* Fetch the list of available states.
*/
const getStates = () => {
const url = `${URLS.BASE_URL}/${URLS.GET_STATES}`;
return superagent_1.default.get(url).then((response) => response.body.states);
};
exports.getStates = getStates;
/**
* Fetch districts corresponding to the given state id.
* @param { number } stateId
*/
const getDistricts = (stateId) => {
const url = `${URLS.BASE_URL}/${URLS.GET_DISTRICTS}/${stateId}`;
return superagent_1.default.get(url).then((response) => response.body.districts);
};
exports.getDistricts = getDistricts;
//# sourceMappingURL=index.js.map