npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

appsuite-shared

v0.0.176

Published

Shared libraries for PDS appsuite

Downloads

79

Readme

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.