Skip to content

Commit

Permalink
Merge pull request #14 from indexnetwork/api-integration
Browse files Browse the repository at this point in the history
Standardize item response
  • Loading branch information
serefyarar authored Feb 1, 2024
2 parents fca4661 + d042ef8 commit 4b6aac7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 91 deletions.
2 changes: 1 addition & 1 deletion api/src/controllers/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const removeItem = async (req, res, next) => {

const itemService = new ItemService().setSession(pkpSession);
const item = await itemService.removeItem(indexId, itemId);
res.status(200).json(item);
res.status(204);
} catch (error) {
res.status(500).json({ error: error.message });
}
Expand Down
147 changes: 57 additions & 90 deletions api/src/services/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,50 @@ const getCurrentDateTime = () => moment.utc().toISOString();

import {definition} from "../types/merged-runtime.js";

const indexItemFragment = `
... on IndexItem {
id
indexId
itemId
createdAt
updatedAt
deletedAt
item {
id
__typename
... on WebPage {
title
favicon
url
content
createdAt
updatedAt
deletedAt
}
}
index {
id
title
signerPublicKey
signerFunction
createdAt
updatedAt
deletedAt
}
}`

const transformIndexItem = (indexItem) => {
console.log(indexItem)
const { __typename: type, indexedAt: _, ...rest } = indexItem.item;
return {
type,
node: {
...rest,
indexedAt: indexItem.updatedAt
}
};
};

export class ItemService {
constructor() {
this.client = new ComposeClient({
Expand Down Expand Up @@ -36,12 +80,7 @@ export class ItemService {
}, sorting: { createdAt: DESC}) {
edges {
node {
id
indexId
itemId
createdAt
updatedAt
deletedAt
${indexItemFragment}
}
}
}
Expand All @@ -60,7 +99,7 @@ export class ItemService {
return null;
}

return data.indexItemIndex.edges[0].node;
return transformIndexItem(data.indexItemIndex.edges[0].node);

} catch (error) {
// Log the error and rethrow it for external handling
Expand All @@ -75,36 +114,7 @@ export class ItemService {
const {data, errors} = await this.client.executeQuery(`
{
node(id: "${indexItemId}") {
... on IndexItem {
id
indexId
itemId
createdAt
updatedAt
deletedAt
item {
id
__typename
... on WebPage {
title
favicon
url
content
createdAt
updatedAt
deletedAt
}
}
index {
id
title
signerPublicKey
signerFunction
createdAt
updatedAt
deletedAt
}
}
${indexItemFragment}
}
}`);

Expand All @@ -117,8 +127,7 @@ export class ItemService {
throw new Error('Invalid response data');
}


return data.node;
return transformIndexItem(data.node);

} catch (error) {
// Log the error and rethrow it for external handling
Expand All @@ -144,36 +153,7 @@ export class ItemService {
}
edges {
node {
... on IndexItem {
id
indexId
itemId
createdAt
updatedAt
deletedAt
item {
id
__typename
... on WebPage {
title
favicon
url
content
createdAt
updatedAt
deletedAt
}
}
index {
id
title
signerPublicKey
signerFunction
createdAt
updatedAt
deletedAt
}
}
${indexItemFragment}
}
}
}
Expand All @@ -197,10 +177,7 @@ export class ItemService {

return { //Todo fix itemId to id
endCursor: data.indexItemIndex.pageInfo.endCursor,
items: data.indexItemIndex.edges.map(({ node: { item: { __typename: type, ...rest } } }) => ({
type,
node: rest
})),
items: data.indexItemIndex.edges.map(e => transformIndexItem(e.node)),
}

} catch (error) {
Expand Down Expand Up @@ -234,12 +211,7 @@ export class ItemService {
mutation CreateIndexItem($input: CreateIndexItemInput!) {
createIndexItem(input: $input) {
document {
id
indexId
itemId
createdAt
updatedAt
deletedAt
${indexItemFragment}
}
}
}`, {input: {content}});
Expand All @@ -249,12 +221,13 @@ export class ItemService {
}

// Validate the data response
if (!data || !data.createIndexItem || !data.createIndexItem.document) {

if (!data || !data.createIndexItem || !data.createIndexItem.document || !data.createIndexItem.document.item) {
throw new Error('Invalid response data');
}

// Return the created index document
return data.createIndexItem.document;
return transformIndexItem(data.createIndexItem.document);


} catch (error) {
// Log the error and rethrow it for external handling
Expand Down Expand Up @@ -287,12 +260,7 @@ export class ItemService {
mutation UpdateIndexItem($input: UpdateIndexItemInput!) {
updateIndexItem(input: $input) {
document {
id
indexId
itemId
createdAt
updatedAt
deletedAt
${indexItemFragment}
}
}
}`, {input: {id: indexItem.id, content}});
Expand All @@ -306,8 +274,7 @@ export class ItemService {
throw new Error('Invalid response data');
}

// Return the created index document
return data.updateIndexItem.document;
return true; //transformIndexItem(data.updateIndexItem.document);;

} catch (error) {
// Log the error and rethrow it for external handling
Expand Down

0 comments on commit 4b6aac7

Please sign in to comment.