5.0.2 • Published 4 years ago

ts-transformer-decoder-cast v5.0.2

Weekly downloads
7
License
MIT
Repository
github
Last release
4 years ago

ts-transformer-decoder-cast

A simple macro expander that convert

api.requestAndCast<User>(options)

to

api.request(options, (decoder, data, onError) => decoder.decode('User', data, onError))

Example

Input

import { ICaster } from '../index';

class Decoder implements ICaster {
  // do not use it directly, use cast<T> instead
  decode<T>(typeName: string, data: unknown, onError?: (errors: string[]) => void): T | undefined {
    console.log(`mock convertion from data to '${typeName}'`);
    return data as T;
  }

  decodeArray<T>(
    typeName: string,
    data: unknown,
    onError?: (errors: string[]) => void,
  ): T[] | undefined {
    console.log(`mock convertion from data to '${typeName}[]'`);
    return data as T;
  }
}

type RequestCallback = (decoder: Decoder, data: unknown) => any;

class ApiService {
  decoder = new Decoder();

  request<T>(url: unknown, cb: RequestCallback): T {
    console.log(`fetching data...`);
    const data = url; // mock fetched data
    return cb(this.decoder, data, console.error);
  }

  requestAndCast<T>(url: unknown): T {
    throw new Error('will be replaced by transformer');
  }
}

const json = [
  {
    name: 'Yang Liu',
    age: 27,
  },
];

interface User {
  name: string;
  age: number;
}

const api = new ApiService();

const user = api.requestAndCast<User>(json[0]);
console.log(user);

const users = api.requestAndCast<User[]>(json);
console.log(users);

Output

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var Decoder = /** @class */ (function() {
  function Decoder() {}
  // do not use it directly, use cast<T> instead
  Decoder.prototype.decode = function(typeName, data) {
    console.log("mock convertion from data to '" + typeName + "'");
    return data;
  };
  Decoder.prototype.decodeArray = function(typeName, data) {
    console.log("mock convertion from data to '" + typeName + "[]'");
    return data;
  };
  return Decoder;
})();
var ApiService = /** @class */ (function() {
  function ApiService() {
    this.decoder = new Decoder();
  }
  ApiService.prototype.request = function(url, cb) {
    console.log('fetching data...');
    var data = url; // mock fetched data
    return cb(this.decoder, data);
  };
  ApiService.prototype.requestAndCast = function(url) {
    throw new Error('will be replaced by transformer');
  };
  return ApiService;
})();
var json = [
  {
    name: 'Yang Liu',
    age: 27,
  },
];
var api = new ApiService();
var user = api.request(json[0], function(c, d) {
  return c.decode('User', d);
});
console.log(user);
var users = api.request(json, function(c, d) {
  return c.decodeArray('User', d);
});
console.log(users);
5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.2

4 years ago

3.1.0

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago