fix APIPaths

This commit is contained in:
Aarni Halinen
2022-01-14 02:16:38 +02:00
parent cf9db40582
commit ed29d11b89
2 changed files with 10 additions and 6 deletions
+6 -6
View File
@@ -11,7 +11,7 @@ export enum APIPath {
FEED = "/feed/:id",
JOBADS = "/jobads/:id",
SIGNUPS = "/signup/:id",
SIGNUPS_EDIT = "/signup/:id/edit/:uuid",
SIGNUPS_EDIT = "/signup/:id/edit",
SIGNUP_FORMS = "/signupForm/:id",
SIGNUP_FORMS_EMAIL = "/signupForm/:id/sendemail",
SIGNUP_FORMS_SIGNUPS = "/signupForm/:id/signups",
@@ -23,12 +23,12 @@ type API = {
path: APIPath;
urlParams?: {
id?: string | number;
uuid?: string;
};
queryParams?: {
limit?: number;
offset?: number;
since?: Date;
uuid?: string;
};
authenticated?: boolean;
};
@@ -53,18 +53,18 @@ const getHeaders = (auth?: boolean): Headers => {
const fillUrlParams = (apiPath: APIPath, params: API["urlParams"] = {}): string => {
const path = apiPath
.split("/") // ["", jobads", ":id"]
.filter(Boolean) // [jobads", ":id"]
.split("/")
.map((urlComponent) => {
// fill in each placeholder component like ':id' with value from params
if (urlComponent.startsWith(":")) {
const key = urlComponent.substring(1);
const value = params[key];
const value = params[key] ?? "";
return value;
}
return urlComponent;
})
.join("/"); // "jobads/:id"
.filter(Boolean)
.join("/");
// code above strips leading and trailing '/' from path
return `/${path}/`;
};
+4
View File
@@ -41,6 +41,8 @@ class SignupApi {
path: APIPath.SIGNUPS_EDIT,
urlParams: {
id,
},
queryParams: {
uuid,
},
}, data);
@@ -56,6 +58,8 @@ class SignupApi {
path: APIPath.SIGNUPS_EDIT,
urlParams: {
id,
},
queryParams: {
uuid,
},
});