async send()

in src/models/basic/remote-post.js [55:95]


  async send(body, stringified = true) {
    if (!this._postIsExecuting) {
      this._pending = true;
      this._postIsExecuting = true;
      const {getPrefix, fetchOptions} = this.constructor;
      const prefix = getPrefix();
      try {
        await defer();
        let {headers} = fetchOptions;
        if (!headers) {
          headers = {};
        }
        fetchOptions.headers = headers;
        let stringifiedBody;
        try {
          stringifiedBody = stringified ? JSON.stringify(body) : body;
        } catch (___) {} // eslint-disable-line
        const response = await fetch(
          `${prefix}${this.url}`,
          {...fetchOptions, body: stringifiedBody},
        );
        if (!this.constructor.noResponse) {
          const data = this.constructor.isJson ? (await response.json()) : (await response.blob());
          this.update({
            data,
            status: response.status,
          });
        } else {
          this.update({status: 'OK', data: {}});
        }
      } catch (e) {
        this.failed = true;
        this.error = e.toString();
      } finally {
        this._postIsExecuting = false;
      }

      this._pending = false;
      this._fetchIsExecuting = false;
    }
  }