Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a more functional API #12

Closed
3 tasks done
StefanoMagrassi opened this issue Jun 18, 2018 · 1 comment
Closed
3 tasks done

Implement a more functional API #12

StefanoMagrassi opened this issue Jun 18, 2018 · 1 comment
Assignees
Milestone

Comments

@StefanoMagrassi
Copy link
Contributor

StefanoMagrassi commented Jun 18, 2018

Description

Thanks to the shift to TS (see #11) we can fully use the fp-ts library in order to implement a more functional API both for base request and api layers.

Warning! The following is just a sketch - please refer to the official documentation once the v1.0.0 will be published

request()

import {TaskEither} from 'fp-ts/lib/TaskEither';

export type Mixed = {[key: string]: any} | object | number | string | boolean | null;

type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';

interface HeadersMap {
  [k: string]: string;
}

class NetworkError {
  public readonly type: 'NetworkError' = 'NetworkError';
  constructor(readonly message: string, readonly uri: USVString) {}
}

class BadUrl {
  public readonly type: 'BadUrl' = 'BadUrl';
  constructor(readonly url: string, readonly response: AppyResponse<Mixed>) {}
}

class BadResponse {
  public readonly type: 'BadResponse' = 'BadResponse';
  constructor(readonly response: AppyResponse<Mixed>) {}
}

type AppyError = NetworkError | BadUrl | BadResponse;

interface AppyResponse<A> {
  headers: HeadersMap;
  status: number;
  statusText: string;
  url: string,
  body: A
}

declare function request(m: Method, u: USVString, o?: RequestInit): TaskEither<AppyError, AppyResponse<Mixed>>

USVString, RequestInit are defined in the standard lib.dom.d.ts

api()

import {TaskEither} from 'fp-ts/lib/TaskEither';
import {Decoder} from 'io-ts';
import {
  AppyError,
  AppyResponse,
  HeadersMap,
  Method,
  Mixed
} from './request';

interface ApiConfig {
  baseUri: string;
  id?: string;
  version?: string;
}

export class DecoderError {
  public readonly type: 'DecoderError' = 'DecoderError';
  constructor(errors: ValidationErrors[]);
}

type ApiError = AppyError | DecoderError;

interface ApOptions<A> extends RequestInit {
  headers?: HeadersMap;
  token: string;
  decoder: Decoder<Mixed, A>
}

type ApiTask<A> = TaskEither<ApiError, AppyResponse<A>>;

interface ApiRequest {
  <A>(m: Method, u: USVString, o:  ApiOptions<A>): ApiTask<A>;
}

interface ApiRequestNoMethod {
  <A>(u: USVString, o:  ApiOptions<A>): ApiTask<A>;
}

interface ApiMethods {
  request: ApiRequest,
  get: ApiRequestNoMethod,
  post: ApiRequestNoMethod,
  put: ApiRequestNoMethod,
  patch: ApiRequestNoMethod,
  del: ApiRequestNoMethod
}

declare function api(c: ApiConfig): ApiMethods

Steps

  • request (implementation + tests)
  • api (implementation + tests)
  • documentation
@StefanoMagrassi StefanoMagrassi added this to the v1.0.0 milestone Jun 18, 2018
@StefanoMagrassi StefanoMagrassi self-assigned this Jun 18, 2018
@StefanoMagrassi StefanoMagrassi mentioned this issue Jun 18, 2018
5 tasks
StefanoMagrassi added a commit that referenced this issue Jun 18, 2018
StefanoMagrassi added a commit that referenced this issue Jun 18, 2018
🤦‍♂️ re #11 #12

This reverts commit d25a703.
StefanoMagrassi added a commit that referenced this issue Jun 20, 2018
StefanoMagrassi added a commit that referenced this issue Jun 22, 2018
StefanoMagrassi added a commit that referenced this issue Jun 27, 2018
It returns instead a specific AppyResponse<A> inteface
which is a simplyfied version of the standard Response.

re #12
StefanoMagrassi added a commit that referenced this issue Jun 27, 2018
- better signature and type model
- `Decoder` has always `Mixed` as input type
- better implementation

re #12
StefanoMagrassi added a commit that referenced this issue Jun 28, 2018
StefanoMagrassi added a commit that referenced this issue Jun 29, 2018
StefanoMagrassi added a commit that referenced this issue Jun 29, 2018
StefanoMagrassi added a commit that referenced this issue Jun 29, 2018
StefanoMagrassi added a commit that referenced this issue Jun 29, 2018
StefanoMagrassi added a commit that referenced this issue Jun 29, 2018
StefanoMagrassi added a commit that referenced this issue Jun 29, 2018
StefanoMagrassi added a commit that referenced this issue Jun 29, 2018
StefanoMagrassi added a commit that referenced this issue Jul 4, 2018
StefanoMagrassi added a commit that referenced this issue Jul 12, 2018
@StefanoMagrassi
Copy link
Contributor Author

resolved by #15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant