92 lines
2.1 KiB
TypeScript
92 lines
2.1 KiB
TypeScript
/* eslint-disable no-console */
|
|
import axios from "axios";
|
|
import JobAd from "@models/JobAd";
|
|
import { getAuthHeader } from "@utils/auth";
|
|
|
|
export const URL = `${process.env.NEXT_PUBLIC_API_URL}/jobads/`;
|
|
|
|
export interface Options {
|
|
onlyNonPast?: boolean;
|
|
limit?: number;
|
|
auth?: boolean;
|
|
}
|
|
|
|
class JobAdApi {
|
|
static async getJobAds(options: Options = {}): Promise<JobAd[]> {
|
|
const { onlyNonPast, limit, auth } = options;
|
|
try {
|
|
const params = {
|
|
since: onlyNonPast ? (new Date()).toISOString() : undefined,
|
|
limit,
|
|
};
|
|
const headers = auth ? { Authorization: getAuthHeader() } : null;
|
|
const resp = await axios.get(`${URL}`, {
|
|
headers,
|
|
params,
|
|
});
|
|
return resp.data.results;
|
|
} catch (err) {
|
|
console.error(err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
static async getJobAd(id: number, auth = false): Promise<JobAd> {
|
|
try {
|
|
const headers = auth ? { Authorization: getAuthHeader() } : null;
|
|
const resp = await axios.get(`${URL}${id}/`, {
|
|
headers,
|
|
});
|
|
return resp.data;
|
|
} catch (err) {
|
|
console.error(err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
static async createJobAd(data: JobAd): Promise<JobAd> {
|
|
try {
|
|
const resp = await axios.post(URL, data, {
|
|
headers: {
|
|
Authorization: getAuthHeader(),
|
|
},
|
|
});
|
|
return resp.data;
|
|
} catch (err) {
|
|
console.error(err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
static async updateJobAd(data: JobAd): Promise<JobAd> {
|
|
try {
|
|
const putUrl = `${URL}${data.id}/`;
|
|
const resp = await axios.put(putUrl, data, {
|
|
headers: {
|
|
Authorization: getAuthHeader(),
|
|
},
|
|
});
|
|
return resp.data;
|
|
} catch (err) {
|
|
console.error(err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
static async deleteJobAd(id: number) {
|
|
try {
|
|
const resp = await axios.delete(`${URL}${id}`, {
|
|
headers: {
|
|
Authorization: getAuthHeader(),
|
|
},
|
|
});
|
|
return resp.data;
|
|
} catch (err) {
|
|
console.error(err);
|
|
throw err;
|
|
}
|
|
}
|
|
}
|
|
|
|
export default JobAdApi;
|