nullcc / ts-retrofit Goto Github PK
View Code? Open in Web Editor NEWA declarative and axios based retrofit implementation for JavaScript and TypeScript.
License: MIT License
A declarative and axios based retrofit implementation for JavaScript and TypeScript.
License: MIT License
Given an endpoint has optional args mapped to query or header params...
@GET("/something")
@Queries({
ping: 'pong'
})
async getSomethingWithOptionalQuery(@Query('since') since?: string, @QueryMap filters?: Record<string, string>): Promise<Response> {
return <Response>{}
};
Request passing some optional args service.getSomethingWithOptionalQuery('foo', { fizz: 'buzz' })
/something?ping=pong&since=foo&fizz=buzz
Request again with no optional args service.getSomethingWithOptionalQuery()
/something?ping=pong&since=foo&fizz=buzz
/something?ping=pong
Same occurs with optional headers
Hi,
please add es6 module support, thanks.
can we use axios-retry with this libary? I couldn't figure out how to do it.
hello dear
Do this module support AbortController and signal config like this ?
const controller = new AbortController();
axios.get('/foo/bar', {
signal: controller.signal
}).then(function(response) {
//...
});
// cancel the request
controller.abort()
Hello everybody!
The example code, in the documentation, for ResponseTransformer decorator is wrong:
https://github.com/nullcc/ts-retrofit#responsetransformer
Best regards and have a nice day,
Daniele
Network Error,without response object assigned to error
Hello,
I've the following api service's method:
@POST('/my-custom-url')
@Multipart
async uploadFileFromBrowser(@Part('file') file: PartDescriptor<File>): Promise<Response<any>> {
return {} as Response;
}
I'm calling it (from an Angular project) with the following code:
async uploadAction() {
const fileInput = document.getElementById('file') as HTMLInputElement;
const files = fileInput.files;
const file = files[0];
const response = await this.api.uploadFileFromBrowser({value: file, filename: file.name});
}
inside baseService.ts, method _resolveParameters
, current line 117:
headers = {...headers, ...(data as FormData).getHeaders()};
data
doesn't have getHeaders
method; Maybe 'cause i'm from a browser and not from node?
I've solved with this code (baseService.ts, method _resolveParameters
, line ~116):
if (headers["content-type"] && headers["content-type"].indexOf("multipart/form-data") !== -1) {
const formData = data as FormData;
if (formData?.getHeaders != null) {
headers = {...headers, ...formData.getHeaders()};
}
}
Best regards,
Daniele
Hello,
I've the following api service's method:
@POST('/my-custom-url')
@Multipart
async uploadFileFromBrowser(@Part('file') file: PartDescriptor<File>): Promise<Response<any>> {
return {} as Response;
}
I'm calling it (from an Angular project) with the following code:
async uploadAction() {
const fileInput = document.getElementById('file') as HTMLInputElement;
const files = fileInput.files;
const file = files[0];
const response = await this.api.uploadFileFromBrowser({value: file, filename: file.name});
}
When I call the uploadAction method the "filename field" is the string: "[object Object]" instead of the "textual name of file".
------WebKitFormBoundaryWY9IohJDLcjoMBw7
Content-Disposition: form-data; name="file"; filename="[object Object]"
Content-Type: image/png
[...]
Inside dataResolver.ts
, method resolve(headers: any, data: any)
the line n. 44 is:
formData.append(key, data[key].value, { filename: data[key].filename });
maybe should be:
formData.append(key, data[key].value, data[key].filename);
Best regards,
Daniele
After npm install or yarn add, there are only "LICENSE.txt", "package.json","README.md" files in node_modules ts-retrofit2 dir
hello dear
how I can pass AxiosRequestConfig as a parameter to services?
It's already implemented in my PR #18
Problem:
Example:
@BasePath(API_PREFIX)
export class InterceptorService extends BaseService {
@GET("/interceptor")
async getParams(): Promise<Response> { return <Response>{} };
@POST("/interceptor")
async createParams(@Body body: Post): Promise<Response> { return <Response>{} };
}
I have to do manually cast an empty object to Response in every method return <Response>{}
Solution:
Use stub-method to automatically cast empty object to expected type.
export const STUB_RESPONSE = <T>() => ({} as T);
Code from example will become:
@BasePath(API_PREFIX)
export class InterceptorService extends BaseService {
@GET("/interceptor")
async getParams(): Promise<Response> { return STUB_RESPONSE() };
@POST("/interceptor")
async createParams(@Body body: Post): Promise<Response> { return STUB_RESPONSE() };
}
Using with a v1+ of axios is not compatible with ts-retrofit
. Couple of changes that I noticed affects ts-retrofit
:
AxiosRequestConfig
- InternalAxiosRequestConfig
AxiosTransformer
- AxiosRequestTransformer
and AxiosResponseTransformer
RT,我已经Fork了代码添加功能了,这两天我把PR提交一下。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.