Skip to content

Commit

Permalink
Merge pull request #415 from mocks-server/release
Browse files Browse the repository at this point in the history
Release v3.10
  • Loading branch information
javierbrea authored Aug 11, 2022
2 parents 8ae2112 + 050f457 commit 48d00d2
Show file tree
Hide file tree
Showing 81 changed files with 1,570 additions and 158 deletions.
3 changes: 3 additions & 0 deletions mocks/admin-api-client-unit-mocks/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
# tests
/coverage
/dist
/*.pem
/*.cert
/*.key

# misc
.DS_Store
Expand Down
7 changes: 7 additions & 0 deletions packages/admin-api-client-data-provider/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Removed
### BREAKING CHANGES

## [6.1.0] - 2022-08-11

### Added

- feat(#390): Add protocol option to `configClient` method


## [6.0.1] - 2022-08-04

### Changed
Expand Down
5 changes: 3 additions & 2 deletions packages/admin-api-client-data-provider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ All exported properties are [`@data-provider/axios`](https://github.com/data-pro

By default, the client is configured to request to http://127.0.0.1:3110/api, based in the [default options of Mocks Server admin API plugin](https://www.mocks-server.org/docs/configuration/options)

You can change both the port and the host using the `configClient` method:
You can change the port, host and protocol using the `configClient` method:

```js
import { configClient } from "@mocks-server/admin-api-client-data-provider";

configClient({
port: 3500,
host: "localhost"
host: "localhost",
protocol: "https",
});
```

Expand Down
2 changes: 1 addition & 1 deletion packages/admin-api-client-data-provider/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mocks-server/admin-api-client-data-provider",
"version": "6.0.1",
"version": "6.1.0",
"description": "Client of @mocks-server/plugin-admin-api built with @data-provider",
"keywords": [
"administration",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sonar.organization=mocks-server
sonar.projectKey=mocks-server_main_admin-api-client-data-provider
sonar.projectName=admin-api-client-data-provider
sonar.projectVersion=6.0.1
sonar.projectVersion=6.1.0

sonar.javascript.file.suffixes=.js
sonar.sourceEncoding=UTF-8
Expand Down
10 changes: 8 additions & 2 deletions packages/admin-api-client-data-provider/src/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { providers } from "@data-provider/core";
import { BASE_PATH, DEFAULT_PORT, DEFAULT_CLIENT_HOST } from "@mocks-server/admin-api-paths";
import {
BASE_PATH,
DEFAULT_PORT,
DEFAULT_CLIENT_HOST,
DEFAULT_PROTOCOL,
} from "@mocks-server/admin-api-paths";

import TAG from "./tag";

const DEFAULT_OPTIONS = {
port: DEFAULT_PORT,
host: DEFAULT_CLIENT_HOST,
protocol: DEFAULT_PROTOCOL,
};

export const configClient = (options) => {
Expand All @@ -15,7 +21,7 @@ export const configClient = (options) => {
};

providers.getByTag(TAG).config({
baseUrl: `http://${finalOptions.host}:${finalOptions.port}${BASE_PATH}`,
baseUrl: `${finalOptions.protocol}://${finalOptions.host}:${finalOptions.port}${BASE_PATH}`,
});
};

Expand Down
5 changes: 5 additions & 0 deletions packages/admin-api-client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Removed
### Breaking change

## [6.2.0] - 2022-08-11

### Added
- feat(#390): Add `https` and `agent` options to the `configClient` method

## [6.1.0] - 2022-08-04

### Added
Expand Down
31 changes: 27 additions & 4 deletions packages/admin-api-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,46 @@ Returns an instance containing next methods:
* `restoreRouteVariants()` - Restore route variants to those defined in current collection.
* `configClient(clientConfig)` - Changes the client configuration.
* `clientConfig` _`<Object>`_ - It should be an object containing any of next properties:
* `port` - Changes the client port.
* `host` - Changes the client host.
* `port` - _`<Number>`_ - Changes the client port. Default is `3110`.
* `host` - _`<String>`_ - Changes the client host. Default is `127.0.0.1`.
* `https` - _`<Boolean>`_ - If `true`, changes the client protocol to "https". Default is `false`.
* `agent` - _`<http.Agent | https.Agent>`_ - A custom agent can be provided. This is useful in Node.js environments in order to make able to request to https APIs with self-signed certificates ([see example below](#requesting-to-apis-with-https-enabled-and-self-signed-certificate)).

## Configuration

By default, clients are configured to request to `http://127.0.0.1:3110/api`, based in the [default options of Mocks Server Plugin Admin API](https://www.mocks-server.org/docs/configuration/options)

You can change both the the host and port of the administration API using the `configClient` method:
You can change the host, port and protocol of the administration API using the `configClient` method:

```js
import { AdminApiClient } from "@mocks-server/admin-api-client";

const apiClient = new AdminApiClient();
apiClient.configClient({
host: "localhost",
port: 3500,
https: true,
});
```

### Requesting to APIs with https enabled and self-signed certificate

When the administration API is started with https enabled using a self-signed certificate, and the client is used in Node.js, a custom agent can be provided in order to avoid unauthorized rejections:

```js
import https from "https";
import { AdminApiClient } from "@mocks-server/admin-api-client";

const httpsAgent = new https.Agent({
rejectUnauthorized: false,
});

const apiClient = new AdminApiClient();
apiClient.configClient({
host: "localhost",
port: 3500
port: 3500,
https: true,
agent: httpsAgent
});
```

Expand Down
1 change: 1 addition & 0 deletions packages/admin-api-client/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = {

// The glob patterns Jest uses to detect test files
testMatch: ["<rootDir>/test/*.spec.js"],
// testMatch: ["<rootDir>/test/client.spec.js"],

// The test environment that will be used for testing
testEnvironment: "node",
Expand Down
2 changes: 1 addition & 1 deletion packages/admin-api-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mocks-server/admin-api-client",
"version": "6.1.0",
"version": "6.2.0",
"description": "Client of @mocks-server/plugin-admin-api",
"keywords": [
"mocks-server",
Expand Down
2 changes: 1 addition & 1 deletion packages/admin-api-client/sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sonar.organization=mocks-server
sonar.projectKey=mocks-server_main_admin-api-client
sonar.projectName=admin-api-client
sonar.projectVersion=6.1.0
sonar.projectVersion=6.2.0

sonar.javascript.file.suffixes=.js
sonar.sourceEncoding=UTF-8
Expand Down
46 changes: 31 additions & 15 deletions packages/admin-api-client/src/entities.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import crossFetch from "cross-fetch";
import type { Response } from "cross-fetch";
import type { AnyObject, ApiClientConfig, Url, ApiPath, Id } from "./types";
import type { AnyObject, ApiClientConfig, Url, ApiPath, Id, Protocol, CrossFetchOptions } from "./types";

import {
BASE_PATH,
Expand All @@ -14,8 +14,13 @@ import {
DEFAULT_PORT,
DEFAULT_CLIENT_HOST,
DEFAULT_PROTOCOL,
HTTPS_PROTOCOL,
} from "@mocks-server/admin-api-paths";

const JSON_HEADERS = {
"Content-Type": "application/json",
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isUndefined(value: any) {
return typeof value === "undefined";
Expand All @@ -33,52 +38,63 @@ function handleResponse(res: Response) {
class ApiClient {
private _host: ApiClientConfig["host"] = DEFAULT_CLIENT_HOST;
private _port: ApiClientConfig["port"] = DEFAULT_PORT;
private _protocol: Protocol = DEFAULT_PROTOCOL;
private _agent?: ApiClientConfig["agent"];

get _baseUrl(): Url {
return `${DEFAULT_PROTOCOL}://${this._host}:${this._port}${BASE_PATH}`;
return `${this._protocol}://${this._host}:${this._port}${BASE_PATH}`;
}

private _fullUrl(apiPath: ApiPath): Url {
return `${this._baseUrl}${apiPath}`;
}

private _addAgent(options: CrossFetchOptions = {}): CrossFetchOptions {
if(this._agent) {
options.agent = this._agent;
}
return options;
}

public config(configuration: ApiClientConfig = {}) {
if (!isUndefined(configuration.host)) {
this._host = configuration.host;
}
if (!isUndefined(configuration.port)) {
this._port = configuration.port;
}
if (!isUndefined(configuration.https)) {
this._protocol = configuration.https ? HTTPS_PROTOCOL : DEFAULT_PROTOCOL;
}
if (!isUndefined(configuration.agent)) {
this._agent = configuration.agent;
}
}

public read(apiPath: ApiPath) {
return crossFetch(this._fullUrl(apiPath)).then(handleResponse);
return crossFetch(this._fullUrl(apiPath), this._addAgent()).then(handleResponse);
}

public patch(apiPath: ApiPath, data: AnyObject) {
return crossFetch(this._fullUrl(apiPath), {
return crossFetch(this._fullUrl(apiPath), this._addAgent({
method: "PATCH",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
},
}).then(handleResponse);
headers: JSON_HEADERS,
})).then(handleResponse);
}

public delete(apiPath: ApiPath) {
return crossFetch(this._fullUrl(apiPath), {
return crossFetch(this._fullUrl(apiPath), this._addAgent({
method: "DELETE",
}).then(handleResponse);
})).then(handleResponse);
}

public create(apiPath: ApiPath, data: AnyObject) {
return crossFetch(this._fullUrl(apiPath), {
return crossFetch(this._fullUrl(apiPath), this._addAgent({
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
},
}).then(handleResponse);
headers: JSON_HEADERS,
})).then(handleResponse);
}
}

Expand Down
12 changes: 10 additions & 2 deletions packages/admin-api-client/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BASE_PATH, DEFAULT_PROTOCOL } from "@mocks-server/admin-api-paths";
import type https from "https";
import type { BASE_PATH } from "@mocks-server/admin-api-paths";

export interface AnyObject {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -13,12 +14,19 @@ export interface MocksServerConfig {
export interface ApiClientConfig {
host?: string;
port?: number;
https?: boolean;
agent?: typeof https.Agent,
}

export type Id = string
export type CollectionId = Id;

export type Protocol = "http" | "https"
export type DelayTime = number;
export type RouteVariantId = `${Id}:${Id}`
export type ApiPath = string
export type Url = `${typeof DEFAULT_PROTOCOL}://${ApiClientConfig["host"]}:${ApiClientConfig["port"]}${typeof BASE_PATH}${ApiPath}`
export type Url = `${Protocol}://${ApiClientConfig["host"]}:${ApiClientConfig["port"]}${typeof BASE_PATH}${ApiPath}`

export interface CrossFetchOptions extends RequestInit {
agent?: typeof https.Agent,
}
Loading

0 comments on commit 48d00d2

Please sign in to comment.