0.0.176 • Published 1 year ago

appsuite-shared v0.0.176

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

appsuite-shared

This repository contains all the classes and functions used in the applications:

  • Portfolio
  • Campaign
  • Licenses

To use it in applications you must publish it as an npm package and add it to applications in package.json:

{
  "dependencies": {
    "appsuite-shared": "0.0.104"
  }
}

Exporting as npm package

To export it as an npm package, set the package version in package.json:

{
  "name": "appsuite-shared",
  "version": "0.0.104",
}

Create the package:

npm run build:

Publish in npm:

npm publish

Dependencies

If any function or class uses third-party dependencies, it must be added in package.json:

{
  "peerDependencies": {
    "auth0": "^3.0.1",
  }
}

Local development

To test before to create package and publish to npm package repository is need follow these commands:

in appsuite-shared directory:

npm link

in the app directory

npm link appsuite-shared

Remember after any change the code execute:

npm run build

Services response

Classes to be used in any application must send a response using the setServiceSuccessResponse function. The parameters are:

  • details (optional): is an array of strings, each element is a detail of the process.
  • message (optional): is a string, message to the user
  • payload: the data to be used in the back-end or front-end application
  • status (optional):: the response code

Handle exceptions

Our exception handling works primarily through propagation: An exception passes from one method to another until a method must return a response to the user in the form of an object. There are two types of exceptions:

  • Exceptions from third-party services: These exceptions are thrown by third-party services (AWS, Auth0, among others)
  • Local exceptions: These exceptions are thrown by services or methods created by the DPS team. ​

Manage exceptions

Exception handling is done through the try-catch block. To use try-catch consider the following:

  1. All functions that working with services (queries to external services, queries to other services, queries to databases) or async operations (read, write or delete file) must have try-cath block:
try {
	// code
} catch (error) {
	console.error(error)
}
  1. There should be, as far as possible, classes that bring together the use of external services (for example: a class that contains all the S3 methods)
  2. Classes that contain external services or those that communicate with third-party services must use the throwExternalServiceError function to throw exceptions.
// If it is a class you must create a method that uses the throwExternalServiceError function

this.throwError = function ({ error, methodName }) {
	throwExternalServiceError({
		error,
		methodName,
		serviceName: this.constructor?.name,
	})
}

// error is the catch block error parameter
// methodName is the method that is throwing the exception
// serviceName is the name of the class where the exception is being thrown

try {
	const client = new S3Client({
		region: this.region,
		credentials: {
			accessKeyId: this.accessKeyId,
			secretAccessKey: this.secretAccessKey,
		},
	})

	return client
} catch (error) {
	this.throwError({
		error,
		methodName: 'getClient',
	})
}
  1. Functions that use external services must use the throwExternalServiceError function
const getUser = async (userId) => {
	try {
		const user = await serviceGetUser(userId)
	} catch (error) {
		throwExternalServiceError({
			error,
			serviceName: 'getUser ',
		})
	}
}
  1. For classes that do not bind external services, they must use the throwLocalError function to make their own method to throw local exceptions:
this.throwLocalError = ({ methodName, error }) => {
	throwLocalError({
		error,
		methodName,
		serviceName: this.constructor?.name,
	})
}

// If it will be used in a method, only use the function throwLocalError
try {
	// code
} catch (error) {
	throwLocalError({
		error,
		methodName: 'my function',
	})
}
  1. If the method of a class is complex, being prone to errors, provision should be made to throw local exceptions.
try {
	const curProjectName = this.projectName ? `-${this.projectName}` : ''
	const fnExt = fileName.split('.').pop()
	const curfileName = `${dsdCrypto.hashSHA256(
		auth0UserId
	)}-${type}${curProjectName}.${fnExt}`
	const imgBuffer = await fsReadFile(url)
	const respUpload = await this.manageFilesAws.uploadFile({
		bucket: this.s3Bucket,
		data: imgBuffer,
		key: `${this.s3Bucket}/${curfileName}`,
	})
	fsRemoveFile(url)

	return respUpload
} catch (error) {
	if (error instanceof ExternalServicesError) throw error

	this.throwLocalError({ methodName: 'uploadImage', error })
}

Error response

To send the exception to the front-end the setServiceErrorResponse function must be used. In a class a method must be created using that function:

this.setServiceErrorResponse = ({
	details,
	message = messages.genericErrorCustomTheme,
	stack,
	status,
}) => {
	return setServiceErrorResponse({
		isDev: true,
		details,
		message,
		stack,
		status,
	})
}

setServiceErrorResponse has the isDev parameter, with this parameter set to true the response includes the exception stack. In classes the isDev must be set in the constructor.

0.0.176

1 year ago

0.0.169

1 year ago

0.0.164

1 year ago

0.0.163

1 year ago

0.0.162

1 year ago

0.0.161

1 year ago

0.0.168

1 year ago

0.0.167

1 year ago

0.0.166

1 year ago

0.0.165

1 year ago

0.0.160

1 year ago

0.0.175

1 year ago

0.0.174

1 year ago

0.0.173

1 year ago

0.0.172

1 year ago

0.0.171

1 year ago

0.0.170

1 year ago

0.0.159

1 year ago

0.0.158

1 year ago

0.0.157

1 year ago

0.0.156

1 year ago

0.0.155

1 year ago

0.0.153

1 year ago

0.0.154

1 year ago

0.0.152

1 year ago

0.0.151

1 year ago

0.0.150

1 year ago

0.0.149

1 year ago

0.0.148

1 year ago

0.0.147

2 years ago

0.0.146

2 years ago

0.0.145

2 years ago

0.0.144

2 years ago

0.0.143

2 years ago

0.0.142

2 years ago

0.0.141

2 years ago

0.0.140

2 years ago

0.0.139

2 years ago

0.0.138

2 years ago

0.0.137

2 years ago

0.0.136

2 years ago

0.0.135

2 years ago

0.0.134

2 years ago

0.0.133

2 years ago

0.0.131

2 years ago

0.0.132

2 years ago

0.0.130

2 years ago

0.0.129

2 years ago

0.0.128

2 years ago

0.0.127

2 years ago

0.0.126

2 years ago

0.0.125

2 years ago

0.0.124

2 years ago

0.0.123

2 years ago

0.0.122

2 years ago

0.0.120

2 years ago

0.0.121

2 years ago

0.0.119

2 years ago

0.0.118

2 years ago

0.0.117

2 years ago

0.0.116

2 years ago

0.0.115

2 years ago

0.0.114

2 years ago

0.0.113

2 years ago

0.0.113-a

2 years ago

0.0.112

2 years ago

0.0.111

2 years ago

0.0.110

2 years ago

0.0.86

2 years ago

0.0.87

2 years ago

0.0.88

2 years ago

0.0.89

2 years ago

0.0.105-b

2 years ago

0.0.105-a

2 years ago

0.0.97-d

2 years ago

0.0.97-c

2 years ago

0.0.97-b

2 years ago

0.0.106

2 years ago

0.0.105

2 years ago

0.0.104

2 years ago

0.0.103

2 years ago

0.0.109

2 years ago

0.0.108

2 years ago

0.0.107

2 years ago

0.0.102

2 years ago

0.0.101

2 years ago

0.0.100

2 years ago

0.0.100-b

2 years ago

0.0.104-c

2 years ago

0.0.104-b

2 years ago

0.0.104-d

2 years ago

0.0.95

2 years ago

0.0.96

2 years ago

0.0.97

2 years ago

0.0.98

2 years ago

0.0.99

2 years ago

0.0.90

2 years ago

0.0.91

2 years ago

0.0.92

2 years ago

0.0.93

2 years ago

0.0.94

2 years ago

0.0.92-d

2 years ago

0.0.96-b

2 years ago

0.0.92-a

2 years ago

0.0.92-c

2 years ago

0.0.92-b

2 years ago

0.0.85

2 years ago

0.0.84

2 years ago

0.0.82

2 years ago

0.0.83

2 years ago

0.0.80

2 years ago

0.0.81

2 years ago

0.0.75

2 years ago

0.0.76

2 years ago

0.0.77

2 years ago

0.0.78

2 years ago

0.0.79

2 years ago

0.0.73

3 years ago

0.0.74

3 years ago

0.0.70

3 years ago

0.0.71

3 years ago

0.0.72

3 years ago

0.0.67

3 years ago

0.0.68

3 years ago

0.0.69

3 years ago

0.0.62

3 years ago

0.0.63

3 years ago

0.0.64

3 years ago

0.0.65

3 years ago

0.0.66

3 years ago

0.0.47

3 years ago

0.0.60

3 years ago

0.0.61

3 years ago

0.0.59

3 years ago

0.0.51

3 years ago

0.0.52

3 years ago

0.0.53

3 years ago

0.0.54

3 years ago

0.0.55

3 years ago

0.0.56

3 years ago

0.0.57

3 years ago

0.0.58

3 years ago

0.0.50

3 years ago

0.0.48

3 years ago

0.0.49

3 years ago

0.0.40

3 years ago

0.0.41

3 years ago

0.0.42

3 years ago

0.0.43

3 years ago

0.0.44

3 years ago

0.0.45

3 years ago

0.0.23

3 years ago

0.0.46

3 years ago

0.0.24

3 years ago

0.0.25

3 years ago

0.0.37

3 years ago

0.0.38

3 years ago

0.0.39

3 years ago

0.0.30

3 years ago

0.0.31

3 years ago

0.0.32

3 years ago

0.0.33

3 years ago

0.0.34

3 years ago

0.0.35

3 years ago

0.0.36

3 years ago

0.0.26

3 years ago

0.0.27

3 years ago

0.0.28

3 years ago

0.0.29

3 years ago

0.0.22

3 years ago

0.0.21

3 years ago

0.0.20

3 years ago

0.0.19

3 years ago

0.0.18

3 years ago

0.0.17

3 years ago

0.0.16

3 years ago

0.0.15

3 years ago

0.0.14

3 years ago

0.0.13

3 years ago

0.0.12

3 years ago

0.0.11

3 years ago

0.0.1

3 years ago