Skip to content

Commit

Permalink
Support session recommendations (#13)
Browse files Browse the repository at this point in the history
* reformat configs
* deal with network errors and DRY
* add session recommendation
  • Loading branch information
Maxsior authored May 7, 2023
1 parent 67018c1 commit ad14ab3
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 120 deletions.
1 change: 0 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ trim_trailing_whitespace = true

[*.md]
max_line_length = 0
trim_trailing_whitespace = true
7 changes: 3 additions & 4 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"tabWidth": 2,
"useTabs": false
}

"tabWidth": 2,
"useTabs": false
}
26 changes: 25 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import {
} from "axios-logger";
import {
FeedbackFilter,
GorseException,
ItemNeighborsOptions,
PopularOptions,
RecommendOptions,
SessionRecommendOptions,
UserNeighborsOptions,
} from ".";
import {
Expand Down Expand Up @@ -47,7 +49,12 @@ import {
insertUsers,
updateUser,
} from "./model/user";
import { getLatest, getPopular, getRecommend } from "./model/recommend";
import {
getLatest,
getPopular,
getRecommend,
getSessionRecommend,
} from "./model/recommend";

export { Item, User, Feedback } from "./interfaces";

Expand Down Expand Up @@ -78,6 +85,16 @@ class Gorse<T extends string> {
this.axiosClient.interceptors.request.use(requestLogger, errorLogger);
this.axiosClient.interceptors.response.use(responseLogger, errorLogger);
}

this.axiosClient.interceptors.response.use(undefined, (error) => {
// HTTP errors
if ("response" in error) {
const { response } = error;
throw new GorseException(response.status, response.data);
}
// Network errors
throw new GorseException(-1, error.message);
});
}

// Core functions
Expand All @@ -94,6 +111,13 @@ class Gorse<T extends string> {
return getRecommend(this.axiosClient, options);
}

getSessionRecommend(
feedbackList: Feedback<T>[],
options: SessionRecommendOptions = {}
) {
return getSessionRecommend(this.axiosClient, feedbackList, options);
}

// Feedback

getFeedback(filter: FeedbackFilter<T>, options?: CursorOptions) {
Expand Down
1 change: 1 addition & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export class GorseException extends Error {
code: number;
constructor(code: number, message: string) {
super(message);
this.name = "GorseException";
this.code = code;
}
}
5 changes: 5 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export interface RecommendOptions {
cursorOptions?: OffsetCursorOptions;
}

export interface SessionRecommendOptions {
category?: string;
cursorOptions?: OffsetCursorOptions;
}

export interface ItemNeighborsOptions {
itemId: string;
category?: string;
Expand Down
25 changes: 0 additions & 25 deletions src/model/feedback.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AxiosInstance, AxiosResponse } from "axios";
import { GorseException } from "../error";
import {
CursorOptions,
Feedback,
Expand All @@ -23,10 +22,6 @@ export function getFeedback<T extends string>(
)
.then(({ data }) => {
return data;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}

Expand All @@ -40,10 +35,6 @@ export function deleteFeedback<T extends string>(
)
.then(({ data }) => {
return data;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}

Expand All @@ -60,10 +51,6 @@ export function getFeedbacksByType<T extends string>(
)
.then(({ data }) => {
return data;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}

Expand All @@ -77,10 +64,6 @@ export function getFeedbacks<T extends string>(
})
.then(({ data }) => {
return data;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}

Expand All @@ -92,10 +75,6 @@ export function insertFeedbacks<T extends string>(
.post<Success, AxiosResponse<Success>>(`/feedback`, feedbacksList)
.then(({ data }) => {
return data.RowAffected;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}

Expand All @@ -107,10 +86,6 @@ export function upsertFeedbacks<T extends string>(
.put<Success, AxiosResponse<Success>>(`/feedback`, feedbacksList)
.then(({ data }) => {
return data.RowAffected;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}

Expand Down
37 changes: 0 additions & 37 deletions src/model/item.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AxiosInstance, AxiosResponse } from "axios";
import { GorseException } from "../error";
import {
CursorOptions,
Success,
Expand All @@ -13,10 +12,6 @@ export function upsertItem(axios: AxiosInstance, itemData: Item) {
.post<Success, AxiosResponse<Success>>(`/item`, itemData)
.then(({ data }) => {
return data.RowAffected;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}

Expand All @@ -25,10 +20,6 @@ export function getItem(axios: AxiosInstance, itemId: string) {
.get<Item, AxiosResponse<Item>>(`/item/${itemId}`)
.then(({ data }) => {
return data;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}

Expand All @@ -37,10 +28,6 @@ export function deleteItem(axios: AxiosInstance, itemId: string) {
.delete<Success, AxiosResponse<Success>>(`/item/${itemId}`)
.then(({ data }) => {
return data.RowAffected;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}

Expand All @@ -53,10 +40,6 @@ export function updateItem(
.patch<Success, AxiosResponse<Success>>(`/item/${itemId}`, itemData)
.then(({ data }) => {
return data.RowAffected;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}

Expand All @@ -71,10 +54,6 @@ export function insertItemCategory(
)
.then(({ data }) => {
return data.RowAffected;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}

Expand All @@ -89,10 +68,6 @@ export function deleteItemCategory(
)
.then(({ data }) => {
return data.RowAffected;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}

Expand All @@ -103,10 +78,6 @@ export function getItems(axios: AxiosInstance, options?: CursorOptions) {
})
.then(({ data }) => {
return data;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}

Expand All @@ -115,10 +86,6 @@ export function upsertItems(axios: AxiosInstance, items: Item[]) {
.post<Success, AxiosResponse<Success>>(`/items`, items)
.then(({ data }) => {
return data.RowAffected;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}

Expand All @@ -135,9 +102,5 @@ export function getItemNeighbors(
)
.then(({ data }) => {
return data;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}
29 changes: 17 additions & 12 deletions src/model/recommend.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { AxiosInstance, AxiosResponse } from "axios";
import { GorseException } from "../error";
import {
Feedback,
LatestOutput,
PopularOptions,
PopularOutput,
RecommendOptions,
SessionRecommendOptions,
} from "../interfaces";

export function getPopular(
Expand All @@ -20,10 +21,6 @@ export function getPopular(
)
.then(({ data }) => {
return data;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}

Expand All @@ -37,10 +34,6 @@ export function getLatest(
})
.then(({ data }) => {
return data;
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
});
}

Expand All @@ -67,9 +60,21 @@ export function getRecommend(
)
.then(({ data }) => {
return data;
});
}

export function getSessionRecommend<T extends string>(
axios: AxiosInstance,
feedbackList: Feedback<T>[] = [],
{ category = "", cursorOptions }: SessionRecommendOptions
) {
return axios
.post(`/session/recommend/${category}`, feedbackList, {
params: {
...cursorOptions,
},
})
.catch((exception) => {
const { response } = exception;
return Promise.reject(new GorseException(response.status, response.data));
.then(({ data }) => {
return data;
});
}
Loading

0 comments on commit ad14ab3

Please sign in to comment.