1.0.15 • Published 6 months ago

gm-http-rest-service v1.0.15

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months 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

6 months ago

1.0.14

6 months ago

1.0.13

6 months ago

1.0.12

6 months ago

1.0.10

11 months ago

1.0.9

11 months ago

1.0.8

11 months ago

1.0.7

11 months ago

1.0.6

12 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago