1.0.15 • Published 2 years ago

gm-http-rest-service v1.0.15

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

gm http rest service

Gm http rest service is Http client.

Exemple with builder

const tokenTest: string|undefined = "adOpbGciOiJI.aukjkjiOiI3NTU3IiwiaWF0IjoxNjgE5Njg3.Q_gmkwevbCfwQWym4PjCo..."

const middlewaresBefore: HttpMiddleware[] = [
  (http) => {
    if (tokenTest) {
      http.headers.set('Authorization', tokenTest);
    } else {
      http.headers.set('Authorization', '');
    }
  },
]

const middlewaresAfter: HttpMiddleware[] = [
  (_, response) => {
    console.log(response, "1")
    return response;
  }
]

const middlewaresSuccess: HttpMiddleware[] = [
  (_, response) => {
    console.log(response, "success")
    return response;
  }
]

const middlewaresError: HttpMiddleware[] = [
  (_, response) => {
    console.log(response, "error")
    return response;
  }
]

function http() {
    return new HttpRestServiceBuilder()
    .withBaseUrl('https://localhost:3000')
    .withMiddlewareBefore(middlewaresBefore) // launch middlewares before send request http (1)
    .withMiddlewareSuccess(middlewaresSuccess) // launch middlewares  response http if success (2)
    .withMiddlewareError(middlewaresError) // launch middlewares  response http if error (2)
    .withMiddlewareAfter(middlewaresAfter) // launch middlewares after send request http (3)
    .build();
}


interface TodoPayload {
  userId: number|null,
  id: number,
  title: string,
  completed: boolean
}

function getTodos() {
    return http().get<TodoPayload[]>("/todos"));
}

function addTodos(todo: TodoPayload) {
    return http().post<boolean>("/todos", todo));
}

function updateTodos(id: number, todo: TodoPayload) {
    return http().put<boolean>(`/todos/${id}`, todo));
}

function deleteTodos(id: number) {
    return http().delete<boolean>(`/todos/${id}`));
}

const todos = getTodos();

```
1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago