Skip to content

Commit

Permalink
run linting
Browse files Browse the repository at this point in the history
  • Loading branch information
dblythy committed Dec 23, 2024
1 parent a3855f1 commit ab03153
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 86 deletions.
4 changes: 2 additions & 2 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4117,7 +4117,7 @@ describe('custom HTTP codes', () => {
}
});

expect(request.status).toBe(201);
expect(request.status).toBe(201);
});

it('should set custom headers in save hook', async () => {
Expand Down Expand Up @@ -4311,4 +4311,4 @@ describe('custom HTTP codes', () => {

expect(response.headers.get('X-Custom-Header')).toBe('custom-value');
});
})
})
1 change: 0 additions & 1 deletion src/PromiseRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import Parse from 'parse/node';
import express from 'express';
import log from './logger';
import { inspect } from 'util';
const Layer = require('express/lib/router/layer');

function validateParameter(key, value) {
Expand Down
2 changes: 1 addition & 1 deletion src/Routers/ClassesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class ClassesRouter extends PromiseRouter {
req.info.context,
triggerResponse
);
return triggerResponse.toResponseObject({response});
return triggerResponse.toResponseObject({ response });
}

// Returns a promise for a {response} object.
Expand Down
45 changes: 22 additions & 23 deletions src/Routers/FunctionsRouter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// FunctionsRouter.js

var Parse = require('parse/node').Parse,
triggers = require('../triggers');
triggers = require('../triggers');

import PromiseRouter from '../PromiseRouter';
import { promiseEnforceMasterKeyAccess, promiseEnsureIdempotency } from '../middlewares';
Expand Down Expand Up @@ -107,14 +107,14 @@ export class FunctionsRouter extends PromiseRouter {
const functionName = req.params.functionName;
const applicationId = req.config.applicationId;
const theFunction = triggers.getFunction(functionName, applicationId);

if (!theFunction) {
throw new Parse.Error(Parse.Error.SCRIPT_FAILED, `Invalid function: "${functionName}"`);
}

let params = Object.assign({}, req.body, req.query);
params = parseParams(params, req.config);

const request = {
params,
master: req.auth?.isMaster,
Expand All @@ -128,18 +128,18 @@ export class FunctionsRouter extends PromiseRouter {
};

const response = new TriggerResponse();

const userString = req.auth.user?.id;

try {
// Run the optional validator
await triggers.maybeRunValidator(request, functionName, req.auth);
// Execute the function
const result = await theFunction(request, response);
if (req.config.logLevels.cloudFunctionSuccess !== 'silent') {
const cleanInput = logger.truncateLogMessage(JSON.stringify(params));
// Run the optional validator
await triggers.maybeRunValidator(request, functionName, req.auth);

// Execute the function
const result = await theFunction(request, response);

if (req.config.logLevels.cloudFunctionSuccess !== 'silent') {
const cleanInput = logger.truncateLogMessage(JSON.stringify(params));
const cleanResult = logger.truncateLogMessage(JSON.stringify(result.response?.result));
logger[req.config.logLevels.cloudFunctionSuccess](
`Ran cloud function ${functionName} for user ${userString} with:\n Input: ${cleanInput}\n Result: ${cleanResult}`,
Expand All @@ -150,15 +150,14 @@ export class FunctionsRouter extends PromiseRouter {
}
);
}

return response.toResponseObject({
response: {
result: Parse._encode(result),
}
});
} catch (error) {
console.log(error);
error = triggers.resolveError(error);

return response.toResponseObject({
response: {
result: Parse._encode(result),
}
});
} catch (err) {
const error = triggers.resolveError(err);
if (req.config.logLevels.cloudFunctionError !== 'silent') {
const cleanInput = logger.truncateLogMessage(JSON.stringify(params));
logger[req.config.logLevels.cloudFunctionError](
Expand All @@ -174,5 +173,5 @@ export class FunctionsRouter extends PromiseRouter {
throw error;
}
}

}
2 changes: 1 addition & 1 deletion src/Routers/UsersRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export class UsersRouter extends ClassesRouter {
}
await req.config.authDataManager.runAfterFind(req, user.authData);

return beforeLoginResponse.toResponseObject({response: user});
return beforeLoginResponse.toResponseObject({ response: user });
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Triggers/ConfigTrigger.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {getRequestObject} from './Trigger';
import { getRequestObject } from './Trigger';
import { maybeRunValidator } from "./Validator";
import { logTriggerSuccessBeforeHook, logTriggerErrorBeforeHook } from './Logger';
import {getClassName} from './Utils';
import {getTrigger} from './TriggerStore';
import { getClassName } from './Utils';
import { getTrigger } from './TriggerStore';
export async function maybeRunGlobalConfigTrigger(triggerType, auth, configObject, originalConfigObject, config, context) {
const GlobalConfigClassName = getClassName(Parse.Config);
const configTrigger = getTrigger(GlobalConfigClassName, triggerType, config.applicationId);
Expand Down Expand Up @@ -36,4 +36,4 @@ export async function maybeRunGlobalConfigTrigger(triggerType, auth, configObjec
}
}
return configObject;
}
}
62 changes: 31 additions & 31 deletions src/Triggers/FileTrigger.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {getClassName} from './Utils';
import {getTrigger} from './TriggerStore';
import {logTriggerSuccessBeforeHook, logTriggerErrorBeforeHook} from './Logger';
import {maybeRunValidator} from './Validator';
import { getClassName } from './Utils';
import { getTrigger } from './TriggerStore';
import { logTriggerSuccessBeforeHook, logTriggerErrorBeforeHook } from './Logger';
import { maybeRunValidator } from './Validator';

export function getRequestFileObject(triggerType, auth, fileObject, config) {
const request = {
Expand Down Expand Up @@ -34,31 +34,31 @@ export async function maybeRunFileTrigger(triggerType, fileObject, config, auth,
if (typeof fileTrigger !== 'function') {
return fileObject;
}
try {
const request = getRequestFileObject(triggerType, auth, fileObject, config);
await maybeRunValidator(request, `${triggerType}.${FileClassName}`, auth);
if (request.skipWithMasterKey) {
return fileObject;
}
const result = await fileTrigger(request, responseObject);
logTriggerSuccessBeforeHook(
triggerType,
'Parse.File',
{ ...fileObject.file.toJSON(), fileSize: fileObject.fileSize },
result,
auth,
config.logLevels.triggerBeforeSuccess
);
return result || fileObject;
} catch (error) {
logTriggerErrorBeforeHook(
triggerType,
'Parse.File',
{ ...fileObject.file.toJSON(), fileSize: fileObject.fileSize },
auth,
error,
config.logLevels.triggerBeforeError
);
throw error;
try {
const request = getRequestFileObject(triggerType, auth, fileObject, config);
await maybeRunValidator(request, `${triggerType}.${FileClassName}`, auth);
if (request.skipWithMasterKey) {
return fileObject;
}
}
const result = await fileTrigger(request, responseObject);
logTriggerSuccessBeforeHook(
triggerType,
'Parse.File',
{ ...fileObject.file.toJSON(), fileSize: fileObject.fileSize },
result,
auth,
config.logLevels.triggerBeforeSuccess
);
return result || fileObject;
} catch (error) {
logTriggerErrorBeforeHook(
triggerType,
'Parse.File',
{ ...fileObject.file.toJSON(), fileSize: fileObject.fileSize },
auth,
error,
config.logLevels.triggerBeforeError
);
throw error;
}
}
21 changes: 8 additions & 13 deletions src/Triggers/Logger.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import logger from '../logger';
export function logTriggerAfterHook(triggerType, className, input, auth, logLevel) {
if (logLevel === 'silent') {
return;
}
const cleanInput = logger.truncateLogMessage(JSON.stringify(input));
logger[logLevel](
`${triggerType} triggered for ${className} for user ${userIdForLog(
auth
)}:\n Input: ${cleanInput}`,
`${triggerType} triggered for ${className} for user ${auth?.user?.id}:\n Input: ${cleanInput}`,
{
className,
triggerType,
user: userIdForLog(auth),
user: auth?.user?.id,
}
);
}
Expand All @@ -22,13 +21,11 @@ export function logTriggerSuccessBeforeHook(triggerType, className, input, resul
const cleanInput = logger.truncateLogMessage(JSON.stringify(input));
const cleanResult = logger.truncateLogMessage(JSON.stringify(result));
logger[logLevel](
`${triggerType} triggered for ${className} for user ${userIdForLog(
auth
)}:\n Input: ${cleanInput}\n Result: ${cleanResult}`,
`${triggerType} triggered for ${className} for user ${auth?.user?.id}:\n Input: ${cleanInput}\n Result: ${cleanResult}`,
{
className,
triggerType,
user: userIdForLog(auth),
user: auth?.user?.id,
}
);
}
Expand All @@ -39,14 +36,12 @@ export function logTriggerErrorBeforeHook(triggerType, className, input, auth, e
}
const cleanInput = logger.truncateLogMessage(JSON.stringify(input));
logger[logLevel](
`${triggerType} failed for ${className} for user ${userIdForLog(
auth
)}:\n Input: ${cleanInput}\n Error: ${JSON.stringify(error)}`,
`${triggerType} failed for ${className} for user ${auth?.user?.id}:\n Input: ${cleanInput}\n Error: ${JSON.stringify(error)}`,
{
className,
triggerType,
error,
user: userIdForLog(auth),
user: auth?.user?.id,
}
);
}
}
4 changes: 2 additions & 2 deletions src/Triggers/QueryTrigger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getTrigger, Types } from "./TriggerStore";
import {getRequestObject} from './Trigger';
import { getRequestObject } from './Trigger';
import { resolveError, toJSONwithObjects } from "./Utils";
import { maybeRunValidator } from "./Validator";
import { logTriggerAfterHook, logTriggerSuccessBeforeHook } from "./Logger";
Expand Down Expand Up @@ -250,4 +250,4 @@ function getRequestQueryObject(triggerType, auth, query, count, config, context,
request['installationId'] = auth.installationId;
}
return request;
}
}
2 changes: 1 addition & 1 deletion src/Triggers/Trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,4 @@ function processTriggerResponse(request, response) {
}

return {};
}
}
2 changes: 1 addition & 1 deletion src/Triggers/TriggerResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export default class TriggerResponse {
location: response.location,
};
}
}
}
2 changes: 1 addition & 1 deletion src/Triggers/TriggerStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,4 @@ export function getValidator(functionName, applicationId) {

export function getJob(jobName, applicationId) {
return get(Category.Jobs, jobName, applicationId);
}
}
6 changes: 3 additions & 3 deletions src/Triggers/Validator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {getValidator} from './TriggerStore'
import {resolveError} from './Utils'
import { getValidator } from './TriggerStore'
import { resolveError } from './Utils'
export function maybeRunValidator(request, functionName, auth) {
const theValidator = getValidator(functionName, Parse.applicationId);
if (!theValidator) {
Expand Down Expand Up @@ -195,4 +195,4 @@ async function builtInTriggerValidator(options, request, auth) {
}
await Promise.all(optionPromises);
}
}
}
4 changes: 2 additions & 2 deletions src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { _unregisterAll, getTrigger, Types, triggerExists, addTrigger, addFuncti
import { maybeRunTrigger, getRequestObject } from "./Triggers/Trigger";
import { getClassName, inflate, resolveError, toJSONwithObjects } from "./Triggers/Utils";
import { maybeRunQueryTrigger,maybeRunAfterFindTrigger } from "./Triggers/QueryTrigger";
import {maybeRunValidator} from "./Triggers/Validator";
import { maybeRunValidator } from "./Triggers/Validator";
import { maybeRunFileTrigger } from "./Triggers/FileTrigger";
import { maybeRunGlobalConfigTrigger } from "./Triggers/ConfigTrigger";

Expand All @@ -27,4 +27,4 @@ export {
maybeRunAfterFindTrigger,
toJSONwithObjects,
runLiveQueryEventHandlers
}
}

0 comments on commit ab03153

Please sign in to comment.