https://github.com/coolerfall/revival
A RESTful http client for typescript based on XMLHttpRequest.
https://github.com/coolerfall/revival
restful typescript xmlhttprequest
Last synced: 3 months ago
JSON representation
A RESTful http client for typescript based on XMLHttpRequest.
- Host: GitHub
- URL: https://github.com/coolerfall/revival
- Owner: coolerfall
- License: mit
- Created: 2017-09-27T03:20:47.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-20T10:53:13.000Z (over 7 years ago)
- Last Synced: 2025-11-27T10:19:47.455Z (5 months ago)
- Topics: restful, typescript, xmlhttprequest
- Language: TypeScript
- Homepage:
- Size: 85 KB
- Stars: 5
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Revival
======
A RESTful http client for react native and typescript based on XMLHttpRequest.
Installation and Usage
====
* Revival only support typescript cause it uses decoratos:
```sh
npm install revival
```
or
```sh
yarn add revival
```
* Add `"experimentalDecorators": true` and `"emitDecoratorMetadata": true` in `tsconfig.json`.
* Create api in your project as below:
```typescript
import { DUMMY, GET, Path } from "revival"
class MyApi {
@GET("user/{id}")
loadAccount(@Path("id") id: string) : Call {
return DUMMY;
}
}
```
* `Revival` will turns all decorators to http request:
```typescript
let revival: Revival = new RevivalBuilder()
.baseUrl("http://test.com/")
.addInterceptor(new LogInterceptor())
.build();
let myapi = revival.create(MyApi);
```
* Then you can call the api with asynchronous mode(with enqueue):
```typescript
let Call = myapi.loadAccount("Vincent");
```
* Revival supports `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, `HEAD`.
* Request url can be set dynamically with `@Path`, and the path must surrounded by { and }:
```typescript
@GET("user/{id}")
loadAccount(@Path("id") id: string) : Call {
return DUMMY;
}
```
Query parameter can be added with `@Query`:
```typescript
@GET("user/{id}")
loadAccount(@Path("id") id: string, @Query("type") type: string) : Call {
return DUMMY;
}
```
For complex query parameters, you can use a object with `@QueryMap`:
```typescript
@GET("user/{id}")
loadAccount(@Path("id") id: string, @QueryMap user: User) : Call {
return DUMMY;
}
```
* An object can be used as a http request body with `@Body`:
```typescript
@POST("user/new")
createUser(@Body user: User) : Call {
return DUMMY;
}
```
* Methods can also be declared to send form-encoded(wtth `@FormUrlEncoded`) and multipart(with `@Multipart`) data:
```typescript
@FormUrlEncoded
@POST("user/update")
updateAccount(@Field("age") age: number, @Field("email") email: string) {
return DUMMY;
}
@Multipart
@PUT("user/update")
updateAccount(@Part("avatar") avatar: Avatar, @Part("name") name: string) {
return DUMMY;
}
```
* Revival will use json to serialize and parse body by default, if you want to convert to something else like xml, you can implement a `ConverterFactory` to serialize and parse body. Use them as below:
```typescript
let revival: Revival = new RevivalBuilder()
.baseUrl("http://test.com/")
.converterFactory(new XmlConvertFactory())
.build();
```
If you just want a `ReviResponse`, you must add `@Raw`:
```typescript
@Raw
@POST("user/password")
createPassword(@Body password: Password) : Call {
return DUMMY;
}
```
* Revival supports call adapter which will turn `Call` to rxjs, promise, you can install them via:
```sh
npm install revival-adapter-rxjs revival-adapter-promise
```
or
```sh
yarn add revival-adapter-rxjs revival-adapter-promise
```
And use them as below:
```typescript
let revival: Revival = new RevivalBuilder()
.baseUrl("http://test.com/")
.addCallAdapter(RxjsCallAdapter.create())
.addCallAdapter(PromiseCallAdapter.create())
.build();
```
Credits
=======
* [Retrofit][1] - Type-safe HTTP client for Android and Java by Square, Inc.
* [Angular/http][2] - One framework. Mobile & desktop.
License
======
```text
MIT License
Copyright (C) 2017-present Vincent Cheung
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
```
[1]: https://github.com/square/retrofit
[2]: https://github.com/angular/angular