add limit query parameter for FeedApi, check other APIs for missing options

This commit is contained in:
Aarni Halinen
2021-04-08 23:07:04 +03:00
parent 5eef7cb015
commit 6007f44eb4
9 changed files with 52 additions and 15 deletions
+5 -1
View File
@@ -8,6 +8,7 @@ export const URL = `${process.env.NEXT_PUBLIC_API_URL}/events/`;
export interface Options { export interface Options {
onlyNonPast?: boolean; onlyNonPast?: boolean;
limit?: number; limit?: number;
offset?: number;
auth?: boolean; auth?: boolean;
} }
@@ -26,11 +27,14 @@ class EventApi {
} }
static async getEvents(options: Options = {}): Promise<Event[]> { static async getEvents(options: Options = {}): Promise<Event[]> {
const { onlyNonPast, limit, auth } = options; const {
onlyNonPast, limit, offset, auth,
} = options;
try { try {
const params = { const params = {
since: onlyNonPast ? (new Date()).toISOString() : undefined, since: onlyNonPast ? (new Date()).toISOString() : undefined,
limit, limit,
offset,
}; };
const headers = auth ? { Authorization: getAuthHeader() } : null; const headers = auth ? { Authorization: getAuthHeader() } : null;
const resp = await axios.get(`${URL}`, { const resp = await axios.get(`${URL}`, {
+10 -2
View File
@@ -6,15 +6,23 @@ import { getAuthHeader } from "@utils/auth";
export const URL = `${process.env.NEXT_PUBLIC_API_URL}/feed/`; export const URL = `${process.env.NEXT_PUBLIC_API_URL}/feed/`;
export interface Options { export interface Options {
limit?: number;
offset?: number;
auth?: boolean; auth?: boolean;
} }
class FeedApi { class FeedApi {
static async getFeed(options: Options = {}): Promise<Post[]> { static async getFeed(options: Options = {}): Promise<Post[]> {
const { auth } = options; const {
limit, offset, auth,
} = options;
const params = {
limit,
offset,
};
const headers = auth ? { Authorization: getAuthHeader() } : null; const headers = auth ? { Authorization: getAuthHeader() } : null;
try { try {
const resp = await axios.get(URL, { headers }); const resp = await axios.get(URL, { params, headers });
return resp.data.results; return resp.data.results;
} catch (err) { } catch (err) {
console.error(err); console.error(err);
+5 -1
View File
@@ -8,16 +8,20 @@ export const URL = `${process.env.NEXT_PUBLIC_API_URL}/jobads/`;
export interface Options { export interface Options {
onlyNonPast?: boolean; onlyNonPast?: boolean;
limit?: number; limit?: number;
offset?: number;
auth?: boolean; auth?: boolean;
} }
class JobAdApi { class JobAdApi {
static async getJobAds(options: Options = {}): Promise<JobAd[]> { static async getJobAds(options: Options = {}): Promise<JobAd[]> {
const { onlyNonPast, limit, auth } = options; const {
onlyNonPast, limit, offset, auth,
} = options;
try { try {
const params = { const params = {
since: onlyNonPast ? (new Date()).toISOString() : undefined, since: onlyNonPast ? (new Date()).toISOString() : undefined,
limit, limit,
offset,
}; };
const headers = auth ? { Authorization: getAuthHeader() } : null; const headers = auth ? { Authorization: getAuthHeader() } : null;
const resp = await axios.get(`${URL}`, { const resp = await axios.get(`${URL}`, {
+5 -3
View File
@@ -6,10 +6,12 @@ import { getAuthHeader } from "@utils/auth";
export const URL = `${process.env.NEXT_PUBLIC_API_URL}/signup/`; export const URL = `${process.env.NEXT_PUBLIC_API_URL}/signup/`;
export const FORM_URL = `${process.env.NEXT_PUBLIC_API_URL}/signupForm/`; export const FORM_URL = `${process.env.NEXT_PUBLIC_API_URL}/signupForm/`;
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface Options { export interface Options {
onlyNonPast?: boolean; // onlyNonPast?: boolean;
limit?: number; // limit?: number;
auth?: boolean; // offset?: number;
// auth?: boolean;
} }
class SignupApi { class SignupApi {
+4 -3
View File
@@ -4,10 +4,11 @@ import Tag from "@models/Tag";
export const URL = `${process.env.NEXT_PUBLIC_API_URL}/tags/`; export const URL = `${process.env.NEXT_PUBLIC_API_URL}/tags/`;
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface Options { export interface Options {
onlyNonPast?: boolean; // limit?: number;
limit?: number; // offset?: number;
auth?: boolean; // auth?: boolean;
} }
class TagApi { class TagApi {
+4 -1
View File
@@ -10,7 +10,9 @@ const fetcher = (url: string, config: AxiosRequestConfig) => axios.get(url, conf
const generateFetchParams = (id = "", options: Options = {}) => { const generateFetchParams = (id = "", options: Options = {}) => {
const url = `${URL}${id}`; const url = `${URL}${id}`;
const { auth, onlyNonPast, limit } = options; const {
auth, onlyNonPast, limit, offset,
} = options;
return { return {
url, url,
@@ -18,6 +20,7 @@ const generateFetchParams = (id = "", options: Options = {}) => {
params: { params: {
since: onlyNonPast ? (new Date()).toISOString() : undefined, since: onlyNonPast ? (new Date()).toISOString() : undefined,
limit, limit,
offset,
}, },
headers: auth ? { Authorization: getAuthHeader() } : null, headers: auth ? { Authorization: getAuthHeader() } : null,
}, },
+5 -1
View File
@@ -10,11 +10,15 @@ const feedFetcher = (url: string, config?: AxiosRequestConfig) => axios.get(url,
const generateFetchParams = (id = "", options: Options = {}) => { const generateFetchParams = (id = "", options: Options = {}) => {
const url = `${URL}${id}`; const url = `${URL}${id}`;
const { auth } = options; const { auth, limit, offset } = options;
return { return {
url, url,
config: { config: {
params: {
limit,
offset,
},
headers: auth ? { Authorization: getAuthHeader() } : null, headers: auth ? { Authorization: getAuthHeader() } : null,
}, },
}; };
+8 -1
View File
@@ -10,11 +10,18 @@ const jobAdFetcher = (url: string, config?: AxiosRequestConfig) => axios.get(url
const generateFetchParams = (id = "", options: Options = {}) => { const generateFetchParams = (id = "", options: Options = {}) => {
const url = `${URL}${id}`; const url = `${URL}${id}`;
const { auth } = options; const {
onlyNonPast, limit, offset, auth,
} = options;
return { return {
url, url,
config: { config: {
params: {
since: onlyNonPast ? (new Date()).toISOString() : undefined,
limit,
offset,
},
headers: auth ? { Authorization: getAuthHeader() } : null, headers: auth ? { Authorization: getAuthHeader() } : null,
}, },
}; };
+6 -2
View File
@@ -15,6 +15,10 @@ const eventOptions = {
limit: 4, limit: 4,
}; };
const feedOptions = {
limit: 4,
};
interface InitialProps { interface InitialProps {
initialEvents: Event[]; initialEvents: Event[];
initialFeed: Post[]; initialFeed: Post[];
@@ -22,7 +26,7 @@ interface InitialProps {
const FrontPage: NextPage<InitialProps> = ({ initialEvents, initialFeed }) => { const FrontPage: NextPage<InitialProps> = ({ initialEvents, initialFeed }) => {
const eventResult = useFetchEvents({ initialData: initialEvents, options: eventOptions }); const eventResult = useFetchEvents({ initialData: initialEvents, options: eventOptions });
const feedResult = useFetchFeed({ initialData: initialFeed }); const feedResult = useFetchFeed({ initialData: initialFeed, options: feedOptions });
return ( return (
<> <>
@@ -38,7 +42,7 @@ const FrontPage: NextPage<InitialProps> = ({ initialEvents, initialFeed }) => {
export const getStaticProps: GetStaticProps<InitialProps> = async () => { export const getStaticProps: GetStaticProps<InitialProps> = async () => {
const initialEvents = await EventApi.getEvents(eventOptions); const initialEvents = await EventApi.getEvents(eventOptions);
const initialFeed = await FeedApi.getFeed(); const initialFeed = await FeedApi.getFeed(feedOptions);
return { return {
props: { props: {
initialEvents, initialEvents,