Skip to content

Commit

Permalink
feat: documentation and github action to publish jsr
Browse files Browse the repository at this point in the history
  • Loading branch information
rathod-sahaab committed Nov 10, 2024
1 parent e022be3 commit 3586e8b
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Build and Test
on:
push:
branches-ignore:
- main

jobs:
build:
Expand Down
41 changes: 41 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Publish JSR

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-22.04
strategy:
matrix:
node-version: [20]
steps:
- uses: actions/checkout@v4
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 9
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'pnpm'
- name: Install dependencies
run: pnpm install
- name: Build
run: pnpm run build
- name: Test
run: pnpm run test

publish:
runs-on: ubuntu-latest
environment: Production
needs: build
permissions:
contents: read
id-token: write # The OIDC ID token is used for authentication with JSR.
steps:
- uses: actions/checkout@v4
- run: npx jsr publish
11 changes: 9 additions & 2 deletions jsr.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
{
"name": "@rathod-sahaab/http-result",
"version": "0.5.0",
"version": "0.5.1",
"exports": {
".": "./src/index.ts"
"./ts-rest": "./src/ts-rest/index.ts"
}
},
"publish": {
"include": [
"LICENSE",
"README.md",
"src/**/*.ts"
]
}
}
30 changes: 30 additions & 0 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@ import { HTTP_ERRORS } from './http.constants'
import { IHttpErrorKind } from './http.types'
import { ErrPayload, ErrType, Result, Success } from './types'

/**
* Helper function to easily create result success type from object
*
* ```ts
* return Ok({value: 5})
* ```
*/
export function Ok<T>(value: T): Success<T> {
return [value, null]
}

/**
* Helper function to easily create result error type from object
*
* ```ts
* return Err('BadRequest', 'Article Too long.')
* ```
*/
export function Err<E extends IHttpErrorKind>(
kind: E,
message: string,
Expand Down Expand Up @@ -56,6 +71,13 @@ type IHttpErrorResultFnsMap = {
) => Result<T, K>
}

/**
* Object containing formatter functions to get ErrPayload
*
* Each function has:
* @param message Message describing error
* @param baseError? Optional: Underlying error that made you create a new error, for something like a stack trace
*/
export const HttpErrors: IHttpErrorFnsMap = (
Object.keys(HTTP_ERRORS) as IHttpErrorKind[]
).reduce<Partial<IHttpErrorFnsMap>>((acc, curr) => {
Expand All @@ -65,6 +87,14 @@ export const HttpErrors: IHttpErrorFnsMap = (
}
}, {}) as IHttpErrorFnsMap

/**
* Same as HttpErrors but for creating result type directly
* Object containing formatter functions to get Result
*
* Each function has:
* @param message Message describing error
* @param baseError? Optional: Underlying error that made you create a new error, for something like a stack trace
*/
export const HttpErrorResults: IHttpErrorResultFnsMap = (
Object.keys(HTTP_ERRORS) as IHttpErrorKind[]
).reduce<Partial<IHttpErrorResultFnsMap>>((acc, curr) => {
Expand Down
8 changes: 8 additions & 0 deletions src/ts-rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export type ITsRestError<S extends IHttpErrorStatus> = {
}
}

/**
* Format http-result error type into a valid ts-rest response
*/
export function tsRestError<E extends IHttpErrorKind>(
err: ErrPayload<E>,
): ITsRestError<IHttpErrorStatusSpecific<E>> {
Expand Down Expand Up @@ -67,6 +70,11 @@ type ITsRestResponseFnsMap = {
: never
}

/**
* Format ouput into ts-rest reponse, define status code with function name.
* Error functions expect a message string, success response functions can
* anything.
*/
export const TsRestResponse: ITsRestResponseFnsMap = (
Object.keys(HTTP) as IHttpKind[]
).reduce<Partial<ITsRestResponseFnsMap>>((acc, curr) => {
Expand Down

0 comments on commit 3586e8b

Please sign in to comment.