-
Notifications
You must be signed in to change notification settings - Fork 1
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
refactor(app): New API service #4381
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { apiURL } from "../config"; | ||
import { createFormDataForFileUpload, ERRORS, YoungDto } from "snu-lib"; | ||
import { capture } from "../sentry"; | ||
|
||
interface Headers { | ||
"x-user-timezone": string; | ||
"Content-Type"?: string; | ||
} | ||
|
||
interface Options { | ||
mode: RequestMode; | ||
credentials: RequestCredentials; | ||
} | ||
|
||
interface ApiResponse { | ||
ok: boolean; | ||
data?: unknown; | ||
code?: string; | ||
} | ||
|
||
interface Api { | ||
get: (path: string) => Promise<ApiResponse>; | ||
post: (path: string, body: unknown) => Promise<ApiResponse>; | ||
put: (path: string, body: unknown) => Promise<ApiResponse>; | ||
remove: (path: string) => Promise<ApiResponse>; | ||
getUser: () => Promise<YoungDto>; | ||
openpdf: (path: string, body: unknown) => Promise<Blob>; | ||
uploadFiles: (path: string, arr: unknown[]) => Promise<ApiResponse>; | ||
} | ||
|
||
class api { | ||
headers: Headers; | ||
options: Options; | ||
|
||
constructor() { | ||
this.headers = { | ||
"x-user-timezone": new Date().getTimezoneOffset().toString(), | ||
}; | ||
this.options = { | ||
mode: "cors", | ||
credentials: "include", | ||
}; | ||
} | ||
|
||
async get(path: string): Promise<ApiResponse> { | ||
const response = await fetch(`${apiURL}${path}`, { | ||
...this.options, | ||
headers: new Headers({ "Content-Type": "application/json", ...this.headers }), | ||
}); | ||
if ([401, 403, 404].includes(response.status)) { | ||
throw new Error(response.status.toString()); | ||
} | ||
const res = await response.json(); | ||
return res; | ||
} | ||
async post(path: string, body: unknown): Promise<ApiResponse> { | ||
const response = await fetch(`${apiURL}${path}`, { | ||
...this.options, | ||
method: "POST", | ||
headers: { "Content-Type": "application/json", ...this.headers }, | ||
body: typeof body === "string" ? body : JSON.stringify(body), | ||
}); | ||
if ([401, 403, 404].includes(response.status)) { | ||
throw new Error(response.status.toString()); | ||
} | ||
const res = await response.json(); | ||
return res; | ||
} | ||
async put(path: string, body: unknown): Promise<ApiResponse> { | ||
const response = await fetch(`${apiURL}${path}`, { | ||
...this.options, | ||
method: "PUT", | ||
headers: { "Content-Type": "application/json", ...this.headers }, | ||
body: typeof body === "string" ? body : JSON.stringify(body), | ||
}); | ||
if ([401, 403, 404].includes(response.status)) { | ||
throw new Error(response.status.toString()); | ||
} | ||
const res = await response.json(); | ||
return res; | ||
} | ||
async remove(path: string): Promise<ApiResponse> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pourquoi remove alors qu'on utilise le verbe http DELETE ? |
||
const response = await fetch(`${apiURL}${path}`, { | ||
...this.options, | ||
method: "DELETE", | ||
headers: new Headers({ "Content-Type": "application/json", ...this.headers }), | ||
}); | ||
if ([401, 403, 404].includes(response.status)) { | ||
throw new Error(response.status.toString()); | ||
} | ||
const res = await response.json(); | ||
return res; | ||
} | ||
|
||
async getUser() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. du coup si c'est une api générique, il ne faudrait pas avoir de service user dedans non ? |
||
const { data: user } = await this.get(`${apiURL}/young/signin_token`); | ||
return user as YoungDto; | ||
} | ||
|
||
async openpdf(path: string, body: unknown) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. downloadPdf plutot non ? |
||
const response = await fetch(`${apiURL}${path}`, { | ||
...this.options, | ||
method: "POST", | ||
headers: new Headers({ "Content-Type": "application/json", ...this.headers }), | ||
body: typeof body === "string" ? body : JSON.stringify(body), | ||
}); | ||
if ([401, 403, 404].includes(response.status)) { | ||
throw new Error(response.status.toString()); | ||
} | ||
const file = await response.blob(); | ||
return file; | ||
} | ||
|
||
async uploadFiles(path: string, arr: unknown[]): Promise<ApiResponse> { | ||
const formData = createFormDataForFileUpload(arr); | ||
const res = await this.post(path, formData); | ||
return res; | ||
} | ||
} | ||
|
||
const API = new api(); | ||
export default API; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utiliser
RouteResponseBody
depuis la snu-libservice-national-universel/packages/lib/src/routes/index.ts
Line 10 in 74d0d20