Fix bug in login functionality

Refactor code to improve performance

Update README with new instructions

Add new feature for user authentication

Fix formatting issues in CSS file

Update dependencies to latest versions

Remove unused variables

Add error handling for edge cases

Fix broken links in documentation

Optimize database queries for faster response times
This commit is contained in:
Simon Larsen 2023-12-19 12:59:03 +00:00
parent fad319016b
commit eccb035bf2
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE

View File

@ -39,42 +39,43 @@ export interface RequestOptions {
}
export default class ModelAPI {
public static async create<TBaseModel extends BaseModel>(
public static async create<TBaseModel extends BaseModel>(data: {
model: TBaseModel,
modelType: { new (): TBaseModel },
modelType: { new(): TBaseModel },
requestOptions?: RequestOptions | undefined
): Promise<
}): Promise<
HTTPResponse<JSONObject | JSONArray | TBaseModel | Array<TBaseModel>>
> {
return await ModelAPI.createOrUpdate(
model,
modelType,
FormType.Create,
{},
requestOptions
return await ModelAPI.createOrUpdate({
model: data.model,
modelType: data.modelType,
formType: FormType.Create,
miscDataProps: {},
requestOptions: data.requestOptions
}
);
}
public static async update<TBaseModel extends BaseModel>(
public static async update<TBaseModel extends BaseModel>(data: {
model: TBaseModel,
modelType: { new (): TBaseModel }
): Promise<
modelType: { new(): TBaseModel }
}): Promise<
HTTPResponse<JSONObject | JSONArray | TBaseModel | Array<TBaseModel>>
> {
return await ModelAPI.createOrUpdate(model, modelType, FormType.Update);
return await ModelAPI.createOrUpdate({ model: data.model, modelType: data.modelType, formType: FormType.Update });
}
public static async updateById<TBaseModel extends BaseModel>(
modelType: { new (): TBaseModel },
public static async updateById<TBaseModel extends BaseModel>(data: {
modelType: { new(): TBaseModel },
id: ObjectID,
data: JSONObject,
apiUrlOverride?: URL,
requestOptions?: RequestOptions
): Promise<
}): Promise<
HTTPResponse<JSONObject | JSONArray | TBaseModel | Array<TBaseModel>>
> {
const model: BaseModel = new modelType();
let apiUrl: URL | null = apiUrlOverride || null;
const model: BaseModel = new data.modelType();
let apiUrl: URL | null = data.apiUrlOverride || null;
if (!apiUrl) {
const apiPath: Route | null = model.getCrudApiPath();
@ -87,7 +88,7 @@ export default class ModelAPI {
apiUrl = URL.fromURL(DASHBOARD_API_URL).addRoute(apiPath);
}
apiUrl = apiUrl.addRoute(`/${id.toString()}`);
apiUrl = apiUrl.addRoute(`/${data.id.toString()}`);
const result: HTTPResponse<
JSONObject | JSONArray | TBaseModel | Array<TBaseModel>
@ -97,9 +98,9 @@ export default class ModelAPI {
HTTPMethod.PUT,
apiUrl,
{
data: data,
data: data.data,
},
this.getCommonHeaders(requestOptions)
this.getCommonHeaders(data.requestOptions)
);
if (result.isSuccess()) {
@ -111,17 +112,17 @@ export default class ModelAPI {
throw result;
}
public static async createOrUpdate<TBaseModel extends BaseModel>(
public static async createOrUpdate<TBaseModel extends BaseModel>(data: {
model: TBaseModel,
modelType: { new (): TBaseModel },
modelType: { new(): TBaseModel },
formType: FormType,
miscDataProps?: JSONObject,
requestOptions?: RequestOptions | undefined
): Promise<ModelAPIHttpResponse<TBaseModel>> {
let apiUrl: URL | null = requestOptions?.overrideRequestUrl || null;
}): Promise<ModelAPIHttpResponse<TBaseModel>> {
let apiUrl: URL | null = data.requestOptions?.overrideRequestUrl || null;
if (!apiUrl) {
const apiPath: Route | null = model.getCrudApiPath();
const apiPath: Route | null = data.model.getCrudApiPath();
if (!apiPath) {
throw new BadDataException(
'This model does not support create or update operations.'
@ -132,10 +133,10 @@ export default class ModelAPI {
}
const httpMethod: HTTPMethod =
formType === FormType.Create ? HTTPMethod.POST : HTTPMethod.PUT;
data.formType === FormType.Create ? HTTPMethod.POST : HTTPMethod.PUT;
if (httpMethod === HTTPMethod.PUT) {
apiUrl = apiUrl.addRoute(`/${model._id}`);
apiUrl = apiUrl.addRoute(`/${data.model._id}`);
}
const apiResult: HTTPErrorResponse | HTTPResponse<TBaseModel> =
@ -144,13 +145,13 @@ export default class ModelAPI {
apiUrl,
{
data: JSONFunctions.serialize(
BaseModel.toJSON(model, modelType)
BaseModel.toJSON(data.model, data.modelType)
),
miscDataProps: miscDataProps || {},
miscDataProps: data.miscDataProps || {},
},
{
...this.getCommonHeaders(requestOptions),
...(requestOptions?.requestHeaders || {}),
...this.getCommonHeaders(data.requestOptions),
...(data.requestOptions?.requestHeaders || {}),
}
);
@ -165,7 +166,7 @@ export default class ModelAPI {
delete (result.data as any)['_miscData'];
}
result.data = BaseModel.fromJSONObject(result.data, modelType);
result.data = BaseModel.fromJSONObject(result.data, data.modelType);
return result;
}
@ -175,16 +176,16 @@ export default class ModelAPI {
throw apiResult;
}
public static async getList<TBaseModel extends BaseModel>(
modelType: { new (): TBaseModel },
public static async getList<TBaseModel extends BaseModel>(data: {
modelType: { new(): TBaseModel },
query: Query<TBaseModel>,
limit: number,
skip: number,
select: Select<TBaseModel>,
sort: Sort<TBaseModel>,
requestOptions?: RequestOptions
): Promise<ListResult<TBaseModel>> {
const model: TBaseModel = new modelType();
}): Promise<ListResult<TBaseModel>> {
const model: TBaseModel = new data.modelType();
const apiPath: Route | null = model.getCrudApiPath();
if (!apiPath) {
throw new BadDataException(
@ -196,8 +197,8 @@ export default class ModelAPI {
.addRoute(apiPath)
.addRoute('/get-list');
if (requestOptions?.overrideRequestUrl) {
apiUrl = requestOptions.overrideRequestUrl;
if (data.requestOptions?.overrideRequestUrl) {
apiUrl = data.requestOptions.overrideRequestUrl;
}
if (!apiUrl) {
@ -207,8 +208,8 @@ export default class ModelAPI {
}
const headers: Dictionary<string> =
this.getCommonHeaders(requestOptions);
if (requestOptions && requestOptions.isMultiTenantRequest) {
this.getCommonHeaders(data.requestOptions);
if (data.requestOptions && data.requestOptions.isMultiTenantRequest) {
headers['isMultiTenantRequest'] = 'true';
}
@ -217,21 +218,21 @@ export default class ModelAPI {
HTTPMethod.POST,
apiUrl,
{
query: JSONFunctions.serialize(query as JSONObject),
select: JSONFunctions.serialize(select as JSONObject),
sort: JSONFunctions.serialize(sort as JSONObject),
query: JSONFunctions.serialize(data.query as JSONObject),
select: JSONFunctions.serialize(data.select as JSONObject),
sort: JSONFunctions.serialize(data.sort as JSONObject),
},
headers,
{
limit: limit.toString(),
skip: skip.toString(),
limit: data.limit.toString(),
skip: data.skip.toString(),
}
);
if (result.isSuccess()) {
const list: Array<TBaseModel> = BaseModel.fromJSONArray(
result.data as JSONArray,
modelType
data.modelType
);
return {
@ -247,12 +248,12 @@ export default class ModelAPI {
throw result;
}
public static async count<TBaseModel extends BaseModel>(
modelType: { new (): TBaseModel },
public static async count<TBaseModel extends BaseModel>(data: {
modelType: { new(): TBaseModel },
query: Query<TBaseModel>,
requestOptions?: RequestOptions | undefined
): Promise<number> {
const model: TBaseModel = new modelType();
}): Promise<number> {
const model: TBaseModel = new data.modelType();
const apiPath: Route | null = model.getCrudApiPath();
if (!apiPath) {
throw new BadDataException(
@ -264,8 +265,8 @@ export default class ModelAPI {
.addRoute(apiPath)
.addRoute('/count');
if (requestOptions?.overrideRequestUrl) {
apiUrl = requestOptions.overrideRequestUrl;
if (data.requestOptions?.overrideRequestUrl) {
apiUrl = data.requestOptions.overrideRequestUrl;
}
if (!apiUrl) {
@ -275,8 +276,8 @@ export default class ModelAPI {
}
const headers: Dictionary<string> =
this.getCommonHeaders(requestOptions);
if (requestOptions && requestOptions.isMultiTenantRequest) {
this.getCommonHeaders(data.requestOptions);
if (data.requestOptions && data.requestOptions.isMultiTenantRequest) {
headers['is-multi-tenant-query'] = 'true';
}
@ -285,7 +286,7 @@ export default class ModelAPI {
HTTPMethod.POST,
apiUrl,
{
query: JSONFunctions.serialize(query as JSONObject),
query: JSONFunctions.serialize(data.query as JSONObject),
},
headers
);
@ -331,13 +332,13 @@ export default class ModelAPI {
return headers;
}
public static async getItem<TBaseModel extends BaseModel>(
modelType: { new (): TBaseModel },
public static async getItem<TBaseModel extends BaseModel>(data: {
modelType: { new(): TBaseModel },
id: ObjectID,
select: Select<TBaseModel>,
requestOptions?: RequestOptions | undefined
): Promise<TBaseModel | null> {
const apiPath: Route | null = new modelType().getCrudApiPath();
}): Promise<TBaseModel | null> {
const apiPath: Route | null = new data.modelType().getCrudApiPath();
if (!apiPath) {
throw new BadDataException(
'This model does not support get operations.'
@ -346,11 +347,11 @@ export default class ModelAPI {
let apiUrl: URL = URL.fromURL(DASHBOARD_API_URL)
.addRoute(apiPath)
.addRoute('/' + id.toString())
.addRoute('/' + data.id.toString())
.addRoute('/get-item');
if (requestOptions?.overrideRequestUrl) {
apiUrl = requestOptions.overrideRequestUrl;
if (data.requestOptions?.overrideRequestUrl) {
apiUrl = data.requestOptions.overrideRequestUrl;
}
if (!apiUrl) {
@ -359,29 +360,31 @@ export default class ModelAPI {
);
}
return this.post<TBaseModel>(modelType, apiUrl, select, requestOptions);
return this.post<TBaseModel>({
modelType: data.modelType, apiUrl: apiUrl, select: data.select, requestOptions: data.requestOptions
});
}
public static async post<TBaseModel extends BaseModel>(
modelType: { new (): TBaseModel },
public static async post<TBaseModel extends BaseModel>(data: {
modelType: { new(): TBaseModel },
apiUrl: URL,
select?: Select<TBaseModel> | undefined,
requestOptions?: RequestOptions | undefined
): Promise<TBaseModel | null> {
}): Promise<TBaseModel | null> {
const result: HTTPResponse<TBaseModel> | HTTPErrorResponse =
await API.fetch<TBaseModel>(
HTTPMethod.POST,
apiUrl,
data.apiUrl,
{
select: JSONFunctions.serialize(select as JSONObject) || {},
select: JSONFunctions.serialize(data.select as JSONObject) || {},
},
this.getCommonHeaders(requestOptions)
this.getCommonHeaders(data.requestOptions)
);
if (result.isSuccess()) {
return BaseModel.fromJSONObject(
result.data as JSONObject,
modelType
data.modelType
);
}
@ -390,12 +393,12 @@ export default class ModelAPI {
throw result;
}
public static async deleteItem<TBaseModel extends BaseModel>(
modelType: { new (): TBaseModel },
public static async deleteItem<TBaseModel extends BaseModel>(data: {
modelType: { new(): TBaseModel },
id: ObjectID,
requestOptions?: RequestOptions | undefined
): Promise<void> {
const apiPath: Route | null = new modelType().getCrudApiPath();
}): Promise<void> {
const apiPath: Route | null = new data.modelType().getCrudApiPath();
if (!apiPath) {
throw new BadDataException(
'This model does not support delete operations.'
@ -404,7 +407,7 @@ export default class ModelAPI {
const apiUrl: URL = URL.fromURL(DASHBOARD_API_URL)
.addRoute(apiPath)
.addRoute('/' + id.toString());
.addRoute('/' + data.id.toString());
if (!apiUrl) {
throw new BadDataException(
@ -417,7 +420,7 @@ export default class ModelAPI {
HTTPMethod.DELETE,
apiUrl,
undefined,
this.getCommonHeaders(requestOptions)
this.getCommonHeaders(data.requestOptions)
);
if (result.isSuccess()) {
@ -432,8 +435,8 @@ export default class ModelAPI {
private static checkStatusCode<TBaseModel extends BaseModel>(
result:
| HTTPResponse<
TBaseModel | JSONObject | JSONArray | Array<TBaseModel>
>
TBaseModel | JSONObject | JSONArray | Array<TBaseModel>
>
| HTTPErrorResponse
): void {
if (result.statusCode === 406) {