Skip to content
This repository has been archived by the owner on Feb 13, 2025. It is now read-only.

Commit

Permalink
Route definition based requests (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad authored Feb 13, 2025
1 parent bfa91ec commit 83c5423
Show file tree
Hide file tree
Showing 10 changed files with 606 additions and 122 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,38 @@ const response = await sendGet(client, {

if non-JSON responses are expected, the library will return null, if not, it will throw an error.

### API contract-based requests

`frontend-http-client` supports using API contracts, created with `@lokalise/universal-ts-utils/api-contracts/apiContracts` in order to make fully type-safe HTTP requests.

Usage example:

```ts
import { somePostRouteDefinition, someGetRouteDefinition } from 'some-service-api-contracts'
import { sendByPayloadRoute } from '@lokalise/frontend-http-client'
import wretch from 'wretch'

const client = wretch(BASE_URL)

const responseBody1 = await sendByPayloadRoute(client, somePostRouteDefinition, {
pathParams: {
userId: 1,
},
body: {
isActive: true,
},
})

const responseBody2 = await sendByGetRoute(client, someGetRouteDefinition, {
pathParams: {
userId: 1,
},
queryParams: {
id: 'testId',
},
})
```

## Credits

This library is brought to you by a joint effort of Lokalise engineers:
Expand Down
24 changes: 12 additions & 12 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
"extends": ["./node_modules/@lokalise/biome-config/configs/biome-base.jsonc"],
"linter": {
"rules": {
"performance": {
"noBarrelFile": "off",
"noReExportAll": "off"
},
"style": {
"noUnusedTemplateLiteral": "off"
}
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
"extends": ["./node_modules/@lokalise/biome-config/configs/biome-base.jsonc"],
"linter": {
"rules": {
"performance": {
"noBarrelFile": "off",
"noReExportAll": "off"
},
"style": {
"noUnusedTemplateLiteral": "off"
}
}
}
}
}
12 changes: 11 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
export { sendPost, sendGet, sendPut, sendDelete, sendPatch, UNKNOWN_SCHEMA } from './src/client.js'
export {
sendPost,
sendGet,
sendPut,
sendDelete,
sendPatch,
sendByGetRoute,
sendByPayloadRoute,
sendByDeleteRoute,
UNKNOWN_SCHEMA,
} from './src/client.js'
123 changes: 62 additions & 61 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,63 +1,64 @@
{
"name": "@lokalise/frontend-http-client",
"version": "2.1.0",
"description": "Opinionated HTTP client for the frontend",
"files": ["dist/**", "LICENSE", "README.md"],
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"module": "./dist/index.mjs",
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
},
"author": {
"name": "Lokalise",
"url": "https://lokalise.com/"
},
"homepage": "https://github.com/lokalise/frontend-http-client",
"repository": {
"type": "git",
"url": "git://github.com/lokalise/frontend-http-client.git"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"build:dev": "tsc",
"build:release": "tsup",
"clean": "rimraf dist",
"lint": "biome check . && tsc --project tsconfig.lint.json --noEmit",
"lint:fix": "biome check --write",
"test": "vitest run --coverage",
"prepublishOnly": "npm run clean && npm run build:release"
},
"dependencies": {
"fast-querystring": "^1.1.2"
},
"peerDependencies": {
"wretch": "^2.8.0",
"zod": "^3.22.0"
},
"devDependencies": {
"@biomejs/biome": "^1.8.3",
"@lokalise/biome-config": "^1.0.0",
"@types/node": "^22.0.0",
"@vitest/coverage-v8": "^3.0.5",
"jest-fail-on-console": "^3.1.2",
"mockttp": "^3.13.0",
"rimraf": "^6.0.0",
"tsup": "8.3.6",
"typescript": "~5.7.2",
"vitest": "^3.0.5"
},
"keywords": ["frontend", "web", "browser", "http", "client", "zod", "validation", "typesafe"]
"name": "@lokalise/frontend-http-client",
"version": "2.1.0",
"description": "Opinionated HTTP client for the frontend",
"files": ["dist/**", "LICENSE", "README.md"],
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"module": "./dist/index.mjs",
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
},
"author": {
"name": "Lokalise",
"url": "https://lokalise.com/"
},
"homepage": "https://github.com/lokalise/frontend-http-client",
"repository": {
"type": "git",
"url": "git://github.com/lokalise/frontend-http-client.git"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"build:dev": "tsc",
"build:release": "tsup",
"clean": "rimraf dist",
"lint": "biome check . && tsc --project tsconfig.lint.json --noEmit",
"lint:fix": "biome check --write",
"test": "vitest run --coverage",
"prepublishOnly": "npm run clean && npm run build:release"
},
"dependencies": {
"fast-querystring": "^1.1.2"
},
"peerDependencies": {
"wretch": "^2.8.0",
"zod": "^3.22.0"
},
"devDependencies": {
"@biomejs/biome": "^1.8.3",
"@lokalise/biome-config": "^1.0.0",
"@lokalise/universal-ts-utils": "^3.3.0",
"@types/node": "^22.0.0",
"@vitest/coverage-v8": "^3.0.5",
"jest-fail-on-console": "^3.1.2",
"mockttp": "^3.13.0",
"rimraf": "^6.0.0",
"tsup": "8.3.6",
"typescript": "~5.7.2",
"vitest": "^3.0.5"
},
"keywords": ["frontend", "web", "browser", "http", "client", "zod", "validation", "typesafe"]
}
Loading

0 comments on commit 83c5423

Please sign in to comment.