Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(dsn-parser): mention official doc site #744

Merged
merged 3 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/strange-bulldogs-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@httpx/dsn-parser': patch
---

Fix the documentation home page
11 changes: 7 additions & 4 deletions docs/src/pages/dsn-parser.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Tabs, Tab, Callout } from 'nextra/components';
- 🚀  Error with indicative message / reasons (discriminated union or throwable).
- 🛡️  Don't leak passwords in the error message.
- 🙏  Assertion and typeguard helpers
- 🤗  Ecosystem friendly (ie: [easy integrate with zod](#zod-integration-example)).
- 🤗  Ecosystem friendly (ie: [zod integration](#zod-integration-example)).

## Install

Expand Down Expand Up @@ -112,12 +112,15 @@ if (parsed.success) {
}
```

### Parameters
### Options

```typescript
import { parseDsn, type ParseDsnOptions } from "@httpx/dsn-parser";

const dsn = "mySql://localhost:6379/db";
const parsed = parseDsn(dsn, {
lowercaseDriver: true,
// Overrides, allows to force some values (ParseDsnOptions)
overrides: {
db: "db3",
port: undefined,
Expand All @@ -133,8 +136,8 @@ assert.deepEqual(parsed.value, {

| Params | Type | Description |
| ----------------- | ---------------------- | ----------------------------------------- |
| `lowercaseDriver` | `<boolean>` | Driver name in lowercase, default `false` |
| `overrides` | `DSN must be a string` | |
| `lowercaseDriver` | `boolean` | Driver name in lowercase, default `false` |
| `overrides` | `ParseDsnOptions` | Overrides allows to force specific values |

## Utilities

Expand Down
79 changes: 45 additions & 34 deletions packages/dsn-parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ $ pnpm add @httpx/dsn-parser

## Features

- 🚀&nbsp; Parse individual fields (ie: `driver`, `user`, `password`, `host`...)
- ✨‍&nbsp; Handle query string parameters and converts to boolean and numeric values.
- 👉&nbsp; Parse individual fields (ie: `driver`, `user`, `password`, `host`...)
- 🖖&nbsp; Handle query string parameters and converts to boolean and numeric values.
- 🦄&nbsp; Handle [special characters like](#why--in-password-matters) `/`, `:`... in the password (some libs won't).
- 🧐&nbsp; Error with indicative message / reasons (discriminated union or throwable).
- 🚀&nbsp; Error with indicative message / reasons (discriminated union or throwable).
- 🛡️&nbsp; Don't leak passwords in the error message.
- ⚒️&nbsp; Assertion and typeguard helpers
- 🤗&nbsp; Ecosystem friendly (ie: [easy integrate with zod](#zod-integration-example)).
- 🙏&nbsp; Assertion and typeguard helpers
- 🤗&nbsp; Ecosystem friendly (ie: [zod integration](#zod-integration-example)).

## Quick start
## Documentation

👉 [Official website](https://belgattitude.github.io/httpx/dsn-parser) or [Github Readme](https://github.com/belgattitude/httpx/tree/main/packages/dsn-parser#readme)

## Usage

### parseDsnOrThrow

Expand Down Expand Up @@ -94,12 +98,15 @@ if (parsed.success) {
}
```

## Options
### Options

```typescript
import { parseDsn, type ParseDsnOptions } from "@httpx/dsn-parser";

const dsn = "mySql://localhost:6379/db";
const parsed = parseDsn(dsn, {
lowercaseDriver: true,
// Overrides, allows to force some values (ParseDsnOptions)
overrides: {
db: "db3",
port: undefined,
Expand All @@ -115,8 +122,23 @@ assert.deepEqual(parsed.value, {

| Params | Type | Description |
| ----------------- | ---------------------- | ----------------------------------------- |
| `lowercaseDriver` | `<boolean>` | Driver name in lowercase, default `false` |
| `overrides` | `DSN must be a string` | |
| `lowercaseDriver` | `boolean` | Driver name in lowercase, default `false` |
| `overrides` | `ParseDsnOptions` | Overrides allows to force specific values |

## Utilities

### Typeguard

```typescript
import { isParsableDsn, type ParsableDsn } from "@httpx/dsn-parser";

const dsn = "postgresql://localhost:6379/db";

if (isParsableDsn(dsn)) {
// known to be ParsableDSN
}
```


### Assertion

Expand All @@ -132,18 +154,23 @@ try {
}
```

### Typeguard
### convertJdbcToDsn

Utility to convert [jdbc](https://learn.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url?view=sql-server-ver15) dsn.
Useful for prisma using [sqlserver](https://www.prisma.io/docs/concepts/database-connectors/sql-server#connection-details).

```typescript
import { isParsableDsn, type ParsableDsn } from "@httpx/dsn-parser";
import { convertJdbcToDsn } from "@httpx/dsn-parser";

const dsn = "postgresql://localhost:6379/db";
const jdbcDsn =
"sqlserver://localhost:1433;database=my-db;authentication=default;user=sa;password=pass03$;encrypt=true;trustServerCertificate=true";

if (isParsableDsn(dsn)) {
// known to be ParsableDSN
}
const dsn = convertJdbcToDsn(jdbc);

// -> 'sqlserver://localhost:1433?database=my-db&authentication=default&user=sa&password=pass03$&encrypt=true&trustServerCertificate=true'
```


## DSN parsing

### Requirements
Expand Down Expand Up @@ -231,25 +258,7 @@ if (!parsed.success) {
| `'EMPTY_DSN'` | `DSN cannot be empty` | |
| `'INVALID_PORT'` | `Invalid port: ${port}` | [1-65535] |

## Utilities

### convertJdbcToDsn

Utility to convert [jdbc](https://learn.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url?view=sql-server-ver15) dsn.
Useful for prisma using [sqlserver](https://www.prisma.io/docs/concepts/database-connectors/sql-server#connection-details).

```typescript
import { convertJdbcToDsn } from "@httpx/dsn-parser";

const jdbcDsn =
"sqlserver://localhost:1433;database=my-db;authentication=default;user=sa;password=pass03$;encrypt=true;trustServerCertificate=true";

const dsn = convertJdbcToDsn(jdbc);

// -> 'sqlserver://localhost:1433?database=my-db&authentication=default&user=sa&password=pass03$&encrypt=true&trustServerCertificate=true'
```

## Faq
## Ecosystem

### Zod integration example

Expand All @@ -269,6 +278,8 @@ export const serverEnvSchema = z.object({
serverEnvSchema.parse(process.env);
```

## Faq

### Why '/' in password matters

Some libs (ioredis...) still might fail parsing a password containing '/',
Expand Down
2 changes: 1 addition & 1 deletion packages/dsn-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"name": "Vanvelthem Sébastien",
"url": "https://github.com/belgattitude"
},
"homepage": "https://github.com/belgattitude/httpx/tree/main/packages/dsn-parser#readme",
"homepage": "https://belgattitude.github.io/httpx/dsn-parser",
"repository": {
"type": "git",
"url": "https://github.com/belgattitude/httpx.git",
Expand Down
2 changes: 1 addition & 1 deletion packages/dsn-parser/src/dsn-parser.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const isParsableNumber = (value: unknown): value is number => {
type ValidNetworkPort = number;

export const isValidNetworkPort = (port: number): port is ValidNetworkPort => {
return port < 65_536 && port > 0;
return !Number.isNaN(port) && port < 65_536 && port > 0;
};

export const removeUndefined = (
Expand Down
2 changes: 1 addition & 1 deletion packages/dsn-parser/src/parse-dsn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const dsnRegexp =
const defaultOptions = {
lowercaseDriver: false,
overrides: {},
};
} as const satisfies ParseDsnOptions;

export const parseDsn = (
dsn: unknown,
Expand Down