Skip to content

Commit

Permalink
feat: add public items endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia committed Jul 23, 2021
1 parent f92e9f6 commit f703a79
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
47 changes: 39 additions & 8 deletions src/api/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
buildGetChildrenRoute,
buildGetItemRoute,
buildGetItemsRoute,
buildGetPublicChildrenRoute,
buildGetPublicItemRoute,
buildGetS3MetadataRoute,
buildMoveItemRoute,
buildPostItemRoute,
Expand All @@ -15,20 +17,38 @@ import {
GET_OWN_ITEMS_ROUTE,
SHARE_ITEM_WITH_ROUTE,
} from './routes';
import { DEFAULT_DELETE, DEFAULT_GET, DEFAULT_PATCH, DEFAULT_POST, failOnError } from './utils';
import {
DEFAULT_DELETE,
DEFAULT_GET,
DEFAULT_PATCH,
DEFAULT_POST,
failOnError,
} from './utils';
import { getParentsIdsFromPath } from '../utils/item';
import { ExtendedItem, Item, QueryClientConfig, UUID } from '../types';
import { StatusCodes } from 'http-status-codes';

export const getItem = async (id: UUID, { API_HOST }: QueryClientConfig) => {
const res = await fetch(
`${API_HOST}/${buildGetItemRoute(id)}`,
DEFAULT_GET,
).then(failOnError);
let res = await fetch(`${API_HOST}/${buildGetItemRoute(id)}`, DEFAULT_GET);

// try to fetch public items if cannot access privately
if (res.status === StatusCodes.UNAUTHORIZED) {
res = await fetch(
`${API_HOST}/${buildGetPublicItemRoute(id)}`,
DEFAULT_GET,
).then(failOnError);
}
if (!res.ok) {
throw new Error(res.statusText);
}
const item = await res.json();
return item;
};

export const getItems = async (ids: UUID[], { API_HOST }: QueryClientConfig) => {
export const getItems = async (
ids: UUID[],
{ API_HOST }: QueryClientConfig,
) => {
const res = await fetch(
`${API_HOST}/${buildGetItemsRoute(ids)}`,
DEFAULT_GET,
Expand Down Expand Up @@ -103,10 +123,21 @@ export const getChildren = async (
id: UUID,
{ API_HOST }: QueryClientConfig,
) => {
const res = await fetch(
let res = await fetch(
`${API_HOST}/${buildGetChildrenRoute(id)}`,
DEFAULT_GET,
).then(failOnError);
);

// try to fetch public items if cannot access privately
if (res.status === StatusCodes.UNAUTHORIZED) {
res = await fetch(
`${API_HOST}/${buildGetPublicChildrenRoute(id)}`,
DEFAULT_GET,
).then(failOnError);
}
if (!res.ok) {
throw new Error(res.statusText);
}

const children = await res.json();

Expand Down
15 changes: 13 additions & 2 deletions src/api/member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
buildPatchMember,
} from './routes';
import { Member, QueryClientConfig, UUID } from '../types';
import { StatusCodes } from 'http-status-codes';

export const getMemberBy = async (
{ email }: { email: string },
Expand All @@ -32,9 +33,19 @@ export const getMember = async (
export const getCurrentMember = async ({ API_HOST }: QueryClientConfig) => {
const res = await fetch(`${API_HOST}/${GET_CURRENT_MEMBER_ROUTE}`, {
...DEFAULT_GET,
}).then(failOnError);
});

return res.json();
if (res.ok) {
return res.json();
}

// return valid response for unauthorized requests
// avoid infinite loading induced by failure in react-query
if (res.status === StatusCodes.UNAUTHORIZED) {
return {};
}

throw new Error(res.statusText);
};

export const editMember = async (
Expand Down
3 changes: 3 additions & 0 deletions src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export const buildDeleteItemsRoute = (ids: UUID[]) =>
export const buildGetChildrenRoute = (id: UUID) =>
`${ITEMS_ROUTE}/${id}/children`;
export const buildGetItemRoute = (id: UUID) => `${ITEMS_ROUTE}/${id}`;
export const buildGetPublicItemRoute = (id: UUID) => `p/${ITEMS_ROUTE}/${id}`;
export const buildGetPublicChildrenRoute = (id: UUID) =>
`p/${ITEMS_ROUTE}/${id}/children`;
export const buildGetItemsRoute = (ids: UUID[]) =>
`${ITEMS_ROUTE}?${qs.stringify({ id: ids }, { arrayFormat: 'repeat' })}`;
export const buildMoveItemRoute = (id: UUID) => `${ITEMS_ROUTE}/${id}/move`;
Expand Down

0 comments on commit f703a79

Please sign in to comment.