Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I h8 openapi, No tan stack query support but there is support for fetch #45

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions backend/scripts/generate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* This File Injects Custom Types DO NOT TOUCH WITHOUT PERMISSION
* Note this must be run in conjuction with the bash script otherwise the generation
* */
*/
import fs from "node:fs";
import { resolve } from "node:path";
import openapiTS, { astToString } from "openapi-typescript";
Expand Down
16 changes: 13 additions & 3 deletions frontend/api/client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { paths } from "@/gen/openapi";
import createClient from "openapi-react-query";
import createFetchClient from "openapi-fetch";
import createFetchClient, { Middleware } from "openapi-fetch";
import { API_BASE_URL } from "@/constants/api";

const fetchClient = createFetchClient<paths>({
baseUrl: API_BASE_URL,
});

export const $api = createClient(fetchClient);
// Might need to break this up
const middleware: Middleware = {
async onResponse({ response }) {
if (!response.ok) {
throw new Error("Sample Error");
}
},
};

fetchClient.use(middleware);

export default fetchClient;
40 changes: 17 additions & 23 deletions frontend/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,27 @@ import { API_BASE_URL } from "@/constants/api";
import { CreateUserPayload, User } from "@/types/user";
import { getAuthToken } from "@/utilities/device-token";
import { handleHTTPStatusError, handleNetworkError } from "@/utilities/errors";
import fetchClient from "./client";

export const createUser = async (payload: CreateUserPayload): Promise<User> => {
try {
const token = await getAuthToken();

if (!token) {
throw new Error("Authorization token is missing.");
}
const token = await getAuthToken();

const response = await fetch(`${API_BASE_URL}/users`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(payload),
});

const data = await response.json();
if (!token) {
throw new Error("Authorization token is missing.");
}

if (response.ok) {
return data;
} else {
return handleHTTPStatusError(response.status, data);
}
} catch (error) {
return handleNetworkError(error);
const { data, error, response } = await fetchClient.POST("/api/v1/users", {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: payload,
});

if (response.ok && data) {
return data;
} else {
return handleHTTPStatusError(response.status, error);
}
};

Expand Down
2 changes: 1 addition & 1 deletion frontend/constants/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ if (!Constants.expoConfig?.hostUri && !Constants.expoConfig) {
throw Error("Failed to load Expo Config");
}

export const API_BASE_URL = `http://${Constants.expoConfig.hostUri?.split(":").shift() || "localhost"}:3000/api/v1`;
export const API_BASE_URL = `http://${Constants.expoConfig.hostUri?.split(":").shift() || "localhost"}:3000`;
5 changes: 5 additions & 0 deletions frontend/types/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { components } from "@/gen/openapi";

export type ERROR = components["schemas"]["Error"];
export type VALIDATION_ERROR = components["schemas"]["ValidationError"];
export type API_ERROR = ERROR | VALIDATION_ERROR;
104 changes: 0 additions & 104 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -314,107 +314,3 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/Error"

/api/v1/users/devices:
post:
tags:
- User Endpoints
summary: Register user's device token
description: Registers an Expo Device Token for the currently authenticated user to receive notifications.
security:
- BearerAuth: []
requestBody:
content:
application/json:
schema:
type: object
properties:
expoToken:
type: string
description: The Expo Device Token of the user.
required:
- expoToken
responses:
200:
description: Successfully registered the device token
content:
application/json:
schema:
type: array
items:
type: string
description: Array of user's device tokens
400:
description: Malformed request
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/Error"
- $ref: "#/components/schemas/ValidationError"
401:
description: Unauthorized due to invalid JWT
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
404:
description: User not found
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
500:
description: Internal server error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
delete:
tags:
- User Endpoints
summary: Remove user's device token
description: Removes a registered Expo Device Token for the currently authenticated user to not receive notifications.
security:
- BearerAuth: []
requestBody:
content:
application/json:
schema:
type: object
properties:
expoToken:
type: string
description: The Expo Device Token of the user.
required:
- expoToken
responses:
200:
description: Successfully removed the device token
content:
application/json:
schema:
type: array
items:
type: string
description: Array of user's remaining device tokens
400:
description: Malformed request
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/Error"
- $ref: "#/components/schemas/ValidationError"
401:
description: Unauthorized due to invalid JWT
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
500:
description: Internal server error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
Loading