Skip to content

Commit

Permalink
Merge branch 'main' of github.com:adobe-rnd/spacecat-shared into rum-…
Browse files Browse the repository at this point in the history
…apis
  • Loading branch information
alinarublea committed Dec 14, 2023
2 parents 1bae4a4 + 81001e9 commit 165e377
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 49 deletions.
93 changes: 48 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"@semantic-release/changelog": "6.0.3",
"@semantic-release/git": "10.0.1",
"@semantic-release/npm": "9.0.2",
"@typescript-eslint/eslint-plugin": "6.13.2",
"@typescript-eslint/parser": "6.13.2",
"@typescript-eslint/eslint-plugin": "6.14.0",
"@typescript-eslint/parser": "6.14.0",
"ajv": "8.12.0",
"c8": "8.0.1",
"eslint": "8.55.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/spacecat-shared-rum-api-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@adobe/fetch": "4.1.1",
"@adobe/helix-shared-wrap": "2.0.0",
"@adobe/helix-universal": "4.4.1",
"@adobe/spacecat-shared-utils": "1.3.0",
"@adobe/spacecat-shared-utils": "1.4.0",
"aws4": "1.12.0"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions packages/spacecat-shared-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# [@adobe/spacecat-shared-utils-v1.5.0](https://github.com/adobe-rnd/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.4.0...@adobe/spacecat-shared-utils-v1.5.0) (2023-12-14)


### Features

* http-utils functions ([#49](https://github.com/adobe-rnd/spacecat-shared/issues/49)) ([20e98fa](https://github.com/adobe-rnd/spacecat-shared/commit/20e98faa75d2f439ea5e671937f78bea706ec5d7))

# [@adobe/spacecat-shared-utils-v1.4.0](https://github.com/adobe-rnd/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.3.0...@adobe/spacecat-shared-utils-v1.4.0) (2023-12-12)


Expand Down
7 changes: 7 additions & 0 deletions packages/spacecat-shared-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ The library includes the following utility functions:
- `hasText(str)`: Checks if the given string is not empty.
- `dateAfterDays(number)`: Calculates the date after a specified number of days from the current date.

The library includes the following http utility functions to create `Response` objects:

- `ok(body)`: Creates a 200 response with a JSON body.
- `badRequest(message)`: Creates a 400 response with a JSON body.
- `notFound(message)`: Creates a 404 response with a JSON body.
- `internalServerError(message)`: Creates a 500 response with a JSON body.

## Testing

This library includes a comprehensive test suite to ensure the reliability of the utility functions. To run the tests, use the following command:
Expand Down
5 changes: 4 additions & 1 deletion packages/spacecat-shared-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adobe/spacecat-shared-utils",
"version": "1.4.0",
"version": "1.5.0",
"description": "Shared modules of the Spacecat Services - utils",
"type": "module",
"main": "src/index.js",
Expand Down Expand Up @@ -31,5 +31,8 @@
"devDependencies": {
"chai": "4.3.10",
"sinon": "17.0.1"
},
"dependencies": {
"@adobe/fetch": "4.1.1"
}
}
51 changes: 51 additions & 0 deletions packages/spacecat-shared-utils/src/http-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

import { Response } from '@adobe/fetch';

/**
* Creates a response with a JSON body. Defaults to 200 status.
* @param {object} body - JSON body.
* @param {number} status - Optional status code.
* @param {object} headers - Optional headers.
* @return {Response} Response.
*/
export function createResponse(body, status = 200, headers = {}) {
return new Response(
JSON.stringify(body),
{
headers: { 'content-type': 'application/json; charset=utf-8', ...headers },
status,
},
);
}
export function ok(body) {
return createResponse(body, 200);
}

export function noContent(headers) {
return createResponse('', 204, headers);
}

export function badRequest(message) {
return createResponse({ message }, 400);
}

export function notFound(message) {
return createResponse({ message }, 404);
}

export function internalServerError(message) {
return createResponse({ message }, 500, {
'x-error': `internal server error: ${message}`,
});
}
31 changes: 31 additions & 0 deletions packages/spacecat-shared-utils/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* governing permissions and limitations under the License.
*/

/** UTILITY FUNCTIONS */
export function arrayEquals<T>(a: T[], b: T[]): boolean;

export function hasText(str: string): boolean;
Expand All @@ -35,3 +36,33 @@ export function toBoolean(value: unknown): boolean;
export function isValidUrl(urlString: string): boolean;

export function dateAfterDays(days: string): Date;

/** HTTP UTILS */

/**
* Creates a 200 response with a JSON body.
* @param {object} body - JSON body.
* @return {Response} Response.
*/
export function ok(body: object): Response;

/**
* Creates a 400 response with a JSON body.
* @param {string} message - Error message.
* @return {Response} Response.
*/
export function badRequest(message: string): Response;

/**
* Creates a 404 response with a JSON body.
* @param {string} message - Error message.
* @return {Response} Response.
*/
export function notFound(message: string): Response;

/**
* Creates a 500 response with a JSON body.
* @param {string} message - Error message.
* @return {Response} Response.
*/
export function internalServerError(message: string): Response;
8 changes: 8 additions & 0 deletions packages/spacecat-shared-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ export {
dateAfterDays,
} from './functions.js';

export {
ok,
noContent,
badRequest,
notFound,
internalServerError,
} from './http-utils.js';

export { resolveSecretsName } from './helpers.js';
Loading

0 comments on commit 165e377

Please sign in to comment.