Giter VIP home page Giter VIP logo

ts-retrofit's Issues

Optional query / header params are leaking into subsequent requests

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
    • expected /something?ping=pong

    Same occurs with optional headers

Support AbortController

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()

Multipart

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

Multipart "filename" from browser

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

Proposition - helper function STUB_RESPONSE to automatically case empty object to Response

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() };
}

Support axios v1.x.x

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.