feat(ci): Set up GitHub Actions workflows for CI/CD
This commit is contained in:
parent
32577ade65
commit
7c4140800b
66
.gitea/workflows/default_nottags.yaml
Normal file
66
.gitea/workflows/default_nottags.yaml
Normal file
@ -0,0 +1,66 @@
|
||||
name: Default (not tags)
|
||||
|
||||
on:
|
||||
push:
|
||||
tags-ignore:
|
||||
- '**'
|
||||
|
||||
env:
|
||||
IMAGE: code.foss.global/host.today/ht-docker-node:npmci
|
||||
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@/${{gitea.repository}}.git
|
||||
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
|
||||
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
|
||||
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
|
||||
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
|
||||
|
||||
jobs:
|
||||
security:
|
||||
runs-on: ubuntu-latest
|
||||
continue-on-error: true
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Install pnpm and npmci
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @ship.zone/npmci
|
||||
|
||||
- name: Run npm prepare
|
||||
run: npmci npm prepare
|
||||
|
||||
- name: Audit production dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --prod
|
||||
continue-on-error: true
|
||||
|
||||
- name: Audit development dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --dev
|
||||
continue-on-error: true
|
||||
|
||||
test:
|
||||
if: ${{ always() }}
|
||||
needs: security
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Test stable
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm test
|
||||
|
||||
- name: Test build
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm build
|
124
.gitea/workflows/default_tags.yaml
Normal file
124
.gitea/workflows/default_tags.yaml
Normal file
@ -0,0 +1,124 @@
|
||||
name: Default (tags)
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- '*'
|
||||
|
||||
env:
|
||||
IMAGE: code.foss.global/host.today/ht-docker-node:npmci
|
||||
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@/${{gitea.repository}}.git
|
||||
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
|
||||
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
|
||||
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
|
||||
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
|
||||
|
||||
jobs:
|
||||
security:
|
||||
runs-on: ubuntu-latest
|
||||
continue-on-error: true
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @ship.zone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Audit production dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --prod
|
||||
continue-on-error: true
|
||||
|
||||
- name: Audit development dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --dev
|
||||
continue-on-error: true
|
||||
|
||||
test:
|
||||
if: ${{ always() }}
|
||||
needs: security
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @ship.zone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Test stable
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm test
|
||||
|
||||
- name: Test build
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm build
|
||||
|
||||
release:
|
||||
needs: test
|
||||
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @ship.zone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Release
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm publish
|
||||
|
||||
metadata:
|
||||
needs: test
|
||||
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
continue-on-error: true
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @ship.zone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Code quality
|
||||
run: |
|
||||
npmci command npm install -g typescript
|
||||
npmci npm install
|
||||
|
||||
- name: Trigger
|
||||
run: npmci trigger
|
||||
|
||||
- name: Build docs and upload artifacts
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
pnpm install -g @git.zone/tsdoc
|
||||
npmci command tsdoc
|
||||
continue-on-error: true
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -3,7 +3,6 @@
|
||||
# artifacts
|
||||
coverage/
|
||||
public/
|
||||
pages/
|
||||
|
||||
# installs
|
||||
node_modules/
|
||||
@ -17,4 +16,4 @@ node_modules/
|
||||
dist/
|
||||
dist_*/
|
||||
|
||||
# custom
|
||||
#------# custom
|
137
.gitlab-ci.yml
137
.gitlab-ci.yml
@ -1,137 +0,0 @@
|
||||
# gitzone ci_default
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: '$CI_BUILD_STAGE'
|
||||
|
||||
stages:
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- metadata
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci git mirror
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --production --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=prod --production
|
||||
tags:
|
||||
- docker
|
||||
|
||||
auditDevDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=dev
|
||||
tags:
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# metadata stage
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
allow_failure: true
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci node install lts
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command tsdoc
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
- tags
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
24
.vscode/launch.json
vendored
24
.vscode/launch.json
vendored
@ -2,28 +2,10 @@
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "current file",
|
||||
"type": "node",
|
||||
"command": "npm test",
|
||||
"name": "Run npm test",
|
||||
"request": "launch",
|
||||
"args": [
|
||||
"${relativeFile}"
|
||||
],
|
||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"protocol": "inspector",
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
},
|
||||
{
|
||||
"name": "test.ts",
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"args": [
|
||||
"test/test.ts"
|
||||
],
|
||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"protocol": "inspector",
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
"type": "node-terminal"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
30
changelog.md
Normal file
30
changelog.md
Normal file
@ -0,0 +1,30 @@
|
||||
# Changelog
|
||||
|
||||
## 2024-12-25 - 1.1.0 - feat(ci)
|
||||
Set up GitHub Actions workflows for CI/CD
|
||||
|
||||
- Replaced GitLab CI with GitHub Actions for both tag and non-tag pushes.
|
||||
- Implemented npm audit for security checks on both production and development dependencies.
|
||||
- Enabled npm test and build steps as part of the CI workflow.
|
||||
|
||||
## 2024-05-29 - 1.0.12 - Documentation & Configuration
|
||||
Minor updates focusing on project configuration and descriptive metadata.
|
||||
|
||||
- Updated project description.
|
||||
- Updated tsconfig multiple times for improved TypeScript configurations.
|
||||
- Adjusted npmextra.json configuration for host consistency.
|
||||
|
||||
## 2021-08-19 to 2024-04-14 - 1.0.11 to 1.0.12 - Core Fixes & Maintenance
|
||||
A range of updates focused on core functionality and essential maintenance fixes across versions.
|
||||
|
||||
- Multiple core updates distributed across versions 1.0.7 to 1.0.12.
|
||||
|
||||
## 2019-01-02 to 2021-08-19 - 1.0.5 to 1.0.10 - Core Updates
|
||||
Core functionality was reviewed and updated multiple times to ensure stability and improved performance.
|
||||
|
||||
## 2017-04-06 to 2019-01-02 - 1.0.0 to 1.0.4 - Initial Development & Feature Implementations
|
||||
The initial stages of development including major feature implementations across status codes and basic framework setup.
|
||||
|
||||
- Initial setup completed with basic framework and configuration.
|
||||
- Implemented handling of 1xx to 5xx range of status codes.
|
||||
- Added continuous integration and initial testing protocols.
|
@ -1,33 +0,0 @@
|
||||
# smartstatus
|
||||
|
||||
status information in TypeScript
|
||||
|
||||
## Availabililty
|
||||
|
||||
[![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartstatus)
|
||||
[![git](https://pushrocks.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/smartstatus)
|
||||
[![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartstatus)
|
||||
[![docs](https://pushrocks.gitlab.io/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartstatus/)
|
||||
|
||||
## Status for master
|
||||
|
||||
[![build status](https://GitLab.com/pushrocks/smartstatus/badges/master/build.svg)](https://GitLab.com/pushrocks/smartstatus/commits/master)
|
||||
[![coverage report](https://GitLab.com/pushrocks/smartstatus/badges/master/coverage.svg)](https://GitLab.com/pushrocks/smartstatus/commits/master)
|
||||
[![npm downloads per month](https://img.shields.io/npm/dm/smartstatus.svg)](https://www.npmjs.com/package/smartstatus)
|
||||
[![Dependency Status](https://david-dm.org/pushrocks/smartstatus.svg)](https://david-dm.org/pushrocks/smartstatus)
|
||||
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartstatus/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartstatus/master/dependencies/npm)
|
||||
[![bitHound Code](https://www.bithound.io/github/pushrocks/smartstatus/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartstatus)
|
||||
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
|
||||
|
||||
## Usage
|
||||
|
||||
Use TypeScript for best in class instellisense.
|
||||
|
||||
For further information read the linked docs at the top of this README.
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||
|
||||
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks)
|
@ -8,16 +8,21 @@
|
||||
"shortDescription": "status information in TypeScript",
|
||||
"npmPackagename": "@push.rocks/smartstatus",
|
||||
"license": "MIT",
|
||||
"description": "A TypeScript library for managing HTTP status information, with detailed status classes.",
|
||||
"description": "A TypeScript library for managing HTTP status codes, providing detailed classes for each status to enhance HTTP error and response handling.",
|
||||
"keywords": [
|
||||
"TypeScript",
|
||||
"HTTP status",
|
||||
"HTTP",
|
||||
"status codes",
|
||||
"error handling",
|
||||
"client errors",
|
||||
"server errors",
|
||||
"status codes",
|
||||
"HTTP response",
|
||||
"HTTP request"
|
||||
"response management",
|
||||
"error management",
|
||||
"API development",
|
||||
"express.js",
|
||||
"web applications",
|
||||
"network services",
|
||||
"custom status codes"
|
||||
]
|
||||
}
|
||||
},
|
||||
|
25862
package-lock.json
generated
25862
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
39
package.json
39
package.json
@ -2,29 +2,31 @@
|
||||
"name": "@push.rocks/smartstatus",
|
||||
"version": "1.0.12",
|
||||
"private": false,
|
||||
"description": "A TypeScript library for managing HTTP status information, with detailed status classes.",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"description": "A TypeScript library for managing HTTP status codes, providing detailed classes for each status to enhance HTTP error and response handling.",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./dist_ts/index.js"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"build": "tsbuild --web"
|
||||
"build": "tsbuild --web",
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://code.foss.global/push.rocks/smartstatus.git"
|
||||
"url": "git+https://code.foss.global/push.rocks/smartstatus.git"
|
||||
},
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://gitlab.com/pkunz/smartstatus/issues"
|
||||
"url": "https://code.foss.global/push.rocks/smartstatus/issues"
|
||||
},
|
||||
"homepage": "https://code.foss.global/push.rocks/smartstatus",
|
||||
"homepage": "https://code.foss.global/push.rocks/smartstatus#readme",
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tstest": "^1.0.54",
|
||||
"@pushrocks/tapbundle": "^3.2.14",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
"@git.zone/tsbuild": "^2.1.25",
|
||||
"@git.zone/tsrun": "^1.3.3",
|
||||
"@git.zone/tstest": "^1.0.54",
|
||||
"@push.rocks/tapbundle": "^5.5.3"
|
||||
},
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
@ -43,12 +45,17 @@
|
||||
],
|
||||
"keywords": [
|
||||
"TypeScript",
|
||||
"HTTP status",
|
||||
"HTTP",
|
||||
"status codes",
|
||||
"error handling",
|
||||
"client errors",
|
||||
"server errors",
|
||||
"status codes",
|
||||
"HTTP response",
|
||||
"HTTP request"
|
||||
"response management",
|
||||
"error management",
|
||||
"API development",
|
||||
"express.js",
|
||||
"web applications",
|
||||
"network services",
|
||||
"custom status codes"
|
||||
]
|
||||
}
|
9627
pnpm-lock.yaml
generated
Normal file
9627
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
173
readme.md
173
readme.md
@ -1,15 +1,16 @@
|
||||
# @push.rocks/smartstatus
|
||||
status information in TypeScript
|
||||
|
||||
A TypeScript library for managing HTTP status information, with detailed status classes.
|
||||
|
||||
## Install
|
||||
|
||||
To install `@push.rocks/smartstatus`, you can use npm (or yarn, or pnpm) by running the following command in your terminal:
|
||||
To install `@push.rocks/smartstatus`, use npm (or yarn, or pnpm) by running the following command in your terminal:
|
||||
|
||||
```sh
|
||||
npm install @push.rocks/smartstatus --save
|
||||
```
|
||||
|
||||
Ensure you have TypeScript and a package to work with TypeScript in your project. If not, you might want to add TypeScript and ts-node (for a start) to your project:
|
||||
Ensure you have TypeScript and a package to work with TypeScript in your project. If not, you might want to install TypeScript and ts-node (for development purposes) to your project:
|
||||
|
||||
```sh
|
||||
npm install typescript ts-node --save-dev
|
||||
@ -17,36 +18,48 @@ npm install typescript ts-node --save-dev
|
||||
|
||||
## Usage
|
||||
|
||||
Using `@push.rocks/smartstatus` allows you to handle HTTP status codes more effectively in TypeScript. Below are examples detailing how to utilize this module in various scenarios.
|
||||
The `@push.rocks/smartstatus` library provides a structured and comprehensive way to handle HTTP status codes in TypeScript, enhancing your ability to manage HTTP responses effectively. Below, we outline an extensive set of scenarios demonstrating how you can leverage this module's capabilities to streamline and enrich your response handling from server-side applications to API endpoints.
|
||||
|
||||
### Getting Started
|
||||
|
||||
Ensure you import the module into your TypeScript file:
|
||||
To start using `@push.rocks/smartstatus`, ensure you import the module into your TypeScript files. Here's a basic import statement you can use:
|
||||
|
||||
```typescript
|
||||
import * as smartstatus from '@push.rocks/smartstatus';
|
||||
```
|
||||
|
||||
For more specific imports, such as obtaining particular HTTP status code classes, you may use:
|
||||
|
||||
```typescript
|
||||
import { HttpStatus, Status404, Status200 } from '@push.rocks/smartstatus';
|
||||
```
|
||||
|
||||
### Retrieving a Specific Status
|
||||
|
||||
You can retrieve specific HTTP status information by using its respective class or a convenient method for fetching it by the status code string.
|
||||
The library allows you to retrieve any HTTP status code with its corresponding information quickly. You can do this by using either a class representation for known statuses or using a method that fetches the status by its status code string.
|
||||
|
||||
#### Example: Fetching 404 Not Found Status
|
||||
|
||||
Let's say you wish to work with the HTTP 404 "Not Found" status. You can retrieve its details as follows:
|
||||
|
||||
```typescript
|
||||
import { HttpStatus } from '@push.rocks/smartstatus';
|
||||
|
||||
const notFoundStatus = smartstatus.HttpStatus.getHttpStatusByString('404');
|
||||
const notFoundStatus = HttpStatus.getHttpStatusByString('404');
|
||||
console.log(notFoundStatus.code); // 404
|
||||
console.log(notFoundStatus.text); // Not Found
|
||||
console.log(notFoundStatus.description); // The requested resource could not be found but may be available in the future. Subsequent requests by the client are permissible.
|
||||
```
|
||||
|
||||
This method `getHttpStatusByString` dynamically returns an instance of the respective status class filled with relevant data such as the HTTP code, text, and a descriptive message.
|
||||
The `getHttpStatusByString` function dynamically returns an instance of the respective status class with fields like HTTP code, textual representation, and an insightful description of the status.
|
||||
|
||||
### Handling Errors with HTTP Statuses
|
||||
### Handling Errors Using HTTP Statuses
|
||||
|
||||
When building a web application or an API, handling different HTTP statuses becomes crucial. Here's an example of how you could use `smartstatus` to enrich error handling in your Express.js app:
|
||||
One of the most valuable features of `@push.rocks/smartstatus` is enriching error handling protocols in web applications or APIs. Let's illustrate how smartstatus can be used with an Express.js framework to enhance HTTP response management, specifically handling different HTTP error statuses.
|
||||
|
||||
#### Example: Implementing with Express.js
|
||||
|
||||
Imagine you are developing a web service, and a specific operation fails due to an authorization issue. Here's how you might use smartstatus to handle this error gracefully:
|
||||
|
||||
```typescript
|
||||
import express from 'express';
|
||||
@ -54,52 +67,142 @@ import { HttpStatus } from '@push.rocks/smartstatus';
|
||||
|
||||
const app = express();
|
||||
|
||||
app.get('/some/endpoint', (req, res) => {
|
||||
// Some logic that might fail
|
||||
if (someConditionNotMet) {
|
||||
const unauthorizedStatus = HttpStatus.getHttpStatusByString('401');
|
||||
res.status(unauthorizedStatus.code).json({
|
||||
error: unauthorizedStatus.text,
|
||||
message: unauthorizedStatus.description,
|
||||
});
|
||||
}
|
||||
app.get('/secure/data', (req, res) => {
|
||||
// Simulating an authorization failure condition
|
||||
const userIsAuthorized = false;
|
||||
|
||||
if (!userIsAuthorized) {
|
||||
const unauthorizedStatus = HttpStatus.getHttpStatusByString('401');
|
||||
res.status(unauthorizedStatus.code).json({
|
||||
error: unauthorizedStatus.text,
|
||||
message: unauthorizedStatus.description,
|
||||
});
|
||||
} else {
|
||||
res.status(200).send('Secure Data');
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(3000, () => {
|
||||
console.log('Server running on port 3000');
|
||||
});
|
||||
```
|
||||
|
||||
In the above scenario, if a specific condition is not met (implying some authorization failure), we respond with a 401 Unauthorized status, along with a message detailing the issue.
|
||||
In this express app endpoint, if the user is not authorized to access the resource, it responds with a 401 Unauthorized status. This status code is enriched with a detailed message derived from smartstatus, providing a helpful explanation to API consumers.
|
||||
|
||||
### Extending with Custom Statuses
|
||||
|
||||
While `@push.rocks/smartstatus` provides a structured manner to handle known HTTP statuses, there might be scenarios where you need custom statuses for internal signaling or specialized clients.
|
||||
While `@push.rocks/smartstatus` covers standard HTTP statuses, the library also allows you to define and manage custom status codes tailored to specific application needs. This is particularly useful for internal APIs requiring bespoke statuses.
|
||||
|
||||
Extending or adding new statuses is straightforward:
|
||||
#### Example: Creating a Custom HTTP Status
|
||||
|
||||
Let's say you want to create a unique status for indicating a special condition encountered by your application, such as a "Processing Error" that is represented by code 499. Define a custom status class as follows:
|
||||
|
||||
```typescript
|
||||
import { HttpStatus } from '@push.rocks/smartstatus';
|
||||
|
||||
class Status418 extends HttpStatus {
|
||||
constructor() {
|
||||
super({ code: 418, text: 'I'm a teapot', description: 'The requested entity body is short and stout. Tip me over and pour me out.' });
|
||||
}
|
||||
class Status499 extends HttpStatus {
|
||||
constructor() {
|
||||
super({
|
||||
code: 499,
|
||||
text: 'Processing Error',
|
||||
description:
|
||||
'The server encountered a processing error which prevented it from completing the request.',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Register the custom status
|
||||
HttpStatus.addStatus('418', Status418);
|
||||
// Register the custom status for use
|
||||
HttpStatus.addStatus('499', Status499);
|
||||
|
||||
// Retrieve and use the custom status
|
||||
const customStatus = HttpStatus.getHttpStatusByString('418');
|
||||
console.log(`${customStatus.code} ${customStatus.text}`); // 418 I'm a teapot
|
||||
// Usage of the custom status
|
||||
const processingErrorStatus = HttpStatus.getHttpStatusByString('499');
|
||||
console.log(`${processingErrorStatus.code} ${processingErrorStatus.text}`); // 499 Processing Error
|
||||
```
|
||||
|
||||
This approach ensures that your application can manage both standard and custom status codes effectively, maintaining a clear and expressive way to handle HTTP responses.
|
||||
This custom status can then be used in the same way as predefined statuses in the library, allowing you to handle unique responses efficiently.
|
||||
|
||||
### Conclusion
|
||||
### Comprehensive Examples Across HTTP Status Ranges
|
||||
|
||||
By integrating `@push.rocks/smartstatus` into your TypeScript applications, you gain a powerful tool to manage HTTP status codes, improving the readability and maintainability of your code when dealing with HTTP responses. Whether you are building web applications, APIs, or services, `smartstatus` offers a structured approach to handling success and error states across your application.
|
||||
The `smartstatus` library classifies statuses across different ranges (1xx, 2xx, 3xx, 4xx, and 5xx). Here, we will demonstrate how to handle each of these categories to provide robustness in response management.
|
||||
|
||||
#### Informational Responses (1xx)
|
||||
|
||||
Status codes in the 1xx range inform the client about the progress of the request. These are mostly used in scenarios involving continuation or switching protocols.
|
||||
|
||||
```typescript
|
||||
import { Status100 } from '@push.rocks/smartstatus';
|
||||
|
||||
// Handling 100 Continue status
|
||||
const continueStatus = new Status100();
|
||||
console.log(continueStatus.code, continueStatus.text, continueStatus.description);
|
||||
```
|
||||
|
||||
#### Successful Responses (2xx)
|
||||
|
||||
The 2xx successes indicate that the client's request was received, understood, and accepted. Utilizing these codes effectively can aid in confirming successful API operations.
|
||||
|
||||
```typescript
|
||||
import { Status200, Status201 } from '@push.rocks/smartstatus';
|
||||
|
||||
// 200 OK status for successfully processed requests
|
||||
const okStatus = new Status200();
|
||||
console.log(okStatus.code, okStatus.text, okStatus.description);
|
||||
|
||||
// 201 Created status for resource creation
|
||||
const createdStatus = new Status201();
|
||||
console.log(createdStatus.code, createdStatus.text, createdStatus.description);
|
||||
```
|
||||
|
||||
#### Redirection Messages (3xx)
|
||||
|
||||
Statuses in the 3xx range indicate redirection, offering guidance on how the client can access different resources or follow up with another request.
|
||||
|
||||
```typescript
|
||||
import { Status301, Status302 } from '@push.rocks/smartstatus';
|
||||
|
||||
// Permanent and Temporary redirects
|
||||
const movedPermanentlyStatus = new Status301();
|
||||
console.log(movedPermanentlyStatus.text); // Moved Permanently
|
||||
|
||||
const foundStatus = new Status302();
|
||||
console.log(foundStatus.text); // Found
|
||||
```
|
||||
|
||||
#### Client Error Responses (4xx)
|
||||
|
||||
This class of status codes is intended to inform the client that the error seems to have been caused by the client, such as a malformed request.
|
||||
|
||||
```typescript
|
||||
import { Status400, Status404 } from '@push.rocks/smartstatus';
|
||||
|
||||
// Using 400 Bad Request for malformed requests
|
||||
const badRequestStatus = new Status400();
|
||||
console.log(badRequestStatus.description);
|
||||
|
||||
// Handling 404 Not Found for missing resources
|
||||
const notFoundStatus = new Status404();
|
||||
console.log(notFoundStatus.description);
|
||||
```
|
||||
|
||||
#### Server Error Responses (5xx)
|
||||
|
||||
5xx indicates server-side errors, signaling the server is aware it encountered an error or is otherwise incapable of performing the request.
|
||||
|
||||
```typescript
|
||||
import { Status500, Status503 } from '@push.rocks/smartstatus';
|
||||
|
||||
// Internal server error handling
|
||||
const internalServerErrorStatus = new Status500();
|
||||
console.log(internalServerErrorStatus.text);
|
||||
|
||||
// Service unavailable handling with a 503 response
|
||||
const serviceUnavailableStatus = new Status503();
|
||||
console.log(serviceUnavailableStatus.description);
|
||||
```
|
||||
|
||||
## License and Legal Information
|
||||
|
||||
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||
|
||||
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
|
||||
import * as smartstatus from '../ts/index';
|
||||
import * as smartstatus from '../ts/index.js';
|
||||
|
||||
tap.test('should get a status by codeString', async () => {
|
||||
const status = smartstatus.HttpStatus.getHttpStatusByString('404');
|
||||
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @push.rocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartstatus',
|
||||
version: '1.1.0',
|
||||
description: 'A TypeScript library for managing HTTP status codes, providing detailed classes for each status to enhance HTTP error and response handling.'
|
||||
}
|
@ -1 +1,7 @@
|
||||
export * from './smartstatus.classes.http';
|
||||
export * from './smartstatus.classes.http.1xx.js';
|
||||
export * from './smartstatus.classes.http.2xx.js';
|
||||
export * from './smartstatus.classes.http.3xx.js';
|
||||
export * from './smartstatus.classes.http.4xx.js';
|
||||
export * from './smartstatus.classes.http.5xx.js';
|
||||
|
||||
export * from './smartstatus.classes.http.js';
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { HttpStatus, TStatusGroup } from './smartstatus.classes.http';
|
||||
import { HttpStatus, type TStatusGroup } from './smartstatus.classes.http.js';
|
||||
|
||||
export class Status100 extends HttpStatus {
|
||||
constructor() {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { HttpStatus, TStatusGroup } from './smartstatus.classes.http';
|
||||
import { HttpStatus, type TStatusGroup } from './smartstatus.classes.http.js';
|
||||
|
||||
export class Status200 extends HttpStatus {
|
||||
constructor() {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { HttpStatus, TStatusGroup } from './smartstatus.classes.http';
|
||||
import { HttpStatus, type TStatusGroup } from './smartstatus.classes.http.js';
|
||||
|
||||
export class Status300 extends HttpStatus {
|
||||
constructor() {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { HttpStatus, TStatusGroup } from './smartstatus.classes.http';
|
||||
import { HttpStatus, type TStatusGroup } from './smartstatus.classes.http.js';
|
||||
|
||||
export class Status400 extends HttpStatus {
|
||||
constructor() {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { HttpStatus, TStatusGroup } from './smartstatus.classes.http';
|
||||
import { HttpStatus, type TStatusGroup } from './smartstatus.classes.http.js';
|
||||
|
||||
export class Status500 extends HttpStatus {
|
||||
constructor() {
|
||||
|
@ -10,11 +10,11 @@ export class HttpStatus {
|
||||
try {
|
||||
statusInstance = new HttpStatus.statusMap[codeStringArg]();
|
||||
} catch {
|
||||
console.log('unknown status')
|
||||
console.log('unknown status');
|
||||
return new HttpStatus({
|
||||
code: 0,
|
||||
text: 'unknown status',
|
||||
description: `The status ${codeStringArg} is not known.`
|
||||
description: `The status ${codeStringArg} is not known.`,
|
||||
});
|
||||
}
|
||||
return statusInstance;
|
||||
@ -28,9 +28,3 @@ export class HttpStatus {
|
||||
this.description = optionsArg.description;
|
||||
}
|
||||
}
|
||||
|
||||
export * from './smartstatus.classes.http.1xx';
|
||||
export * from './smartstatus.classes.http.2xx';
|
||||
export * from './smartstatus.classes.http.3xx';
|
||||
export * from './smartstatus.classes.http.4xx';
|
||||
export * from './smartstatus.classes.http.5xx';
|
||||
|
@ -6,9 +6,11 @@
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"esModuleInterop": true,
|
||||
"verbatimModuleSyntax": true
|
||||
"verbatimModuleSyntax": true,
|
||||
"baseUrl": ".",
|
||||
"paths": {}
|
||||
},
|
||||
"exclude": [
|
||||
"dist_*/**/*.d.ts"
|
||||
]
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user