Skip to content

Commit

Permalink
feat: update api
Browse files Browse the repository at this point in the history
  • Loading branch information
DorielRivalet committed Mar 25, 2024
1 parent 8c6ded2 commit 8ac3f8b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 33 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ A data retrieval and translation project for Monster Hunter Frontier Z
It is recommended to host the API yourself. You can do so by doing the following:

1. Fork the [repository](https://github.com/DorielRivalet/ezlion/fork).
2. Deploy the project following the instructions [here](https://vercel.com/docs/deployments/git#deploying-a-git-repository).
3. Select app/ as the root of the project in Vercel Settings.
4. Confirm if it worked correctly by checking the API Reference at `https://yourprojectname.vercel.app/docs`.
2. Deploy the project following the instructions [here](https://vercel.com/docs/deployments/git#deploying-a-git-repository). Select app/ as the root of the project in Vercel Settings.
3. Confirm if it worked correctly by checking the API Reference at `https://yourprojectname.vercel.app/docs`.

### Usage

Expand Down
75 changes: 61 additions & 14 deletions app/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,25 +196,50 @@ type JSONData = {
metadata: { readonly: boolean; repository: string };
};

export function respond(req: Request, url: URL, data: JSONData, id: string = ''): Response {
if (req.method !== 'GET') {
return error(
405,
'The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The Ezlion API is read-only.'
);
}
function handleList(url: URL, data: JSONData) {
// implement queries here
const params = url.searchParams;
const sort = params.get('sort') || 'id'; // Default to 'id' if not specified
const order = params.get('order') || 'ascending'; // Default to 'ascending' if not specified
const page = parseInt(params.get('page') || '1', 10); // Default to page 1 if not specified
const limit = parseInt(params.get('limit') || '10', 10); // Default to 10 items per page if not specified

if (url === undefined) {
return error(
400,
'The server cannot or will not process the request due to something that is perceived to be a client error. The url is undefined.'
);
let results = [...data.results];

// Apply sorting if sort parameter is provided
if (sort) {
results.sort((a, b) => {
if (sort === 'name') {
return order === 'ascending' || order === 'asc'
? a.name.localeCompare(b.name)
: b.name.localeCompare(a.name);
} else {
return order === 'ascending' || order === 'asc' ? a.id - b.id : b.id - a.id;
}
});
}

if (id === '') {
return json(data);
// Apply pagination if page and limit parameters are provided
if (page && limit) {
const startIndex = (page - 1) * limit;
results = results.slice(startIndex, startIndex + limit);
}

// Return the results
return json({
results: results,
metadata: {
readonly: data.metadata.readonly,
repository: data.metadata.repository,
totalItems: data.results.length,
totalPages: Math.ceil(data.results.length / limit),
currentPage: page,
itemsPerPage: limit
}
});
}

function handleId(id: string, data: JSONData) {
const selectedID = parseInt(id, 10);

if (Number.isNaN(selectedID)) {
Expand All @@ -235,3 +260,25 @@ export function respond(req: Request, url: URL, data: JSONData, id: string = '')
);
}
}

export function respond(req: Request, url: URL, data: JSONData, id: string = ''): Response {
if (req.method !== 'GET') {
return error(
405,
'The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The Ezlion API is read-only.'
);
}

if (url === undefined) {
return error(
400,
'The server cannot or will not process the request due to something that is perceived to be a client error. The url is undefined.'
);
}

if (id === '') {
return handleList(url, data);
} else {
return handleId(id, data);
}
}
37 changes: 21 additions & 16 deletions app/src/routes/docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"title": "EZlion API Reference",
"version": "0.2.0",
"summary": "Retrieves information about Monster Hunter Frontier Z.",
"description": "This API returns a list of objects or a single object depending on the endpoint. Each list includes metadata information, and in case of an error it returns the default value in the default key. All information comes from the game Monster Hunter Frontier Z.",
"description": "This API returns a list of objects or a single object depending on the endpoint. Each list includes metadata information. All information comes from the game Monster Hunter Frontier Z.",
"license": {
"name": "MIT",
"url": "https://github.com/DorielRivalet/ezlion/blob/main/LICENSE"
Expand All @@ -18,16 +18,30 @@
"responses": {
"200": {
"description": "A list of monsters in the results key. Includes metadata key."
},
"400": {
"description": "Bad Request. Return application/problem+json. Returns default value in default key."
},
"500": {
"description": "Internal Server Error. Return application/problem+json. Returns default value in default key."
}
}
},
"parameters": [
{
"name": "sort",
"in": "query",
"description": "The category to sort by.",
"required": false,
"schema": {
"type": "string",
"default": "id"
}
},
{
"name": "order",
"in": "query",
"description": "The order of the sorting.",
"required": false,
"schema": {
"type": "string",
"default": "ascending"
}
},
{
"name": "page",
"in": "query",
Expand Down Expand Up @@ -67,15 +81,6 @@
"responses": {
"200": {
"description": "A single monster."
},
"400": {
"description": "Bad Request. Return application/problem+json. Returns default value in default key."
},
"404": {
"description": "Not Found. Return problem+json. Returns default value in default key."
},
"500": {
"description": "Internal Server Error. Return application/problem+json. Returns default value in default key."
}
}
}
Expand Down

0 comments on commit 8ac3f8b

Please sign in to comment.