@proconnect-gouv/proconnect.debounce 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @proconnect-gouv/proconnect.debounce
2
2
 
3
+ ## 1.0.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [#2585](https://github.com/proconnect-gouv/proconnect-identite/pull/2585) [`01ed18b`](https://github.com/proconnect-gouv/proconnect-identite/commit/01ed18bd538420e4736e29311e7f4bd63b1eecc6) Thanks [@douglasduteil](https://github.com/douglasduteil)! - 🔒 Publication via npm trusted publishing (OIDC) au lieu d'un jeton npm
8
+
9
+ ## 1.0.2
10
+
11
+ ### Patch Changes
12
+
13
+ - [#1904](https://github.com/proconnect-gouv/proconnect-identite/pull/1904) [`711aa2f`](https://github.com/proconnect-gouv/proconnect-identite/commit/711aa2fd16b19f6ff2baf5da4a40aaf2a313a42b) Thanks [@BenoitSerrano](https://github.com/BenoitSerrano)! - correction de memory leak
14
+
15
+ - [#1900](https://github.com/proconnect-gouv/proconnect-identite/pull/1900) [`dc33a64`](https://github.com/proconnect-gouv/proconnect-identite/commit/dc33a6428431f5cca6e7d0d554c5a141f8872935) Thanks [@BenoitSerrano](https://github.com/BenoitSerrano)! - ajoute une fonction de ping pour le package debounce
16
+
3
17
  ## 1.0.1
4
18
 
5
19
  ### Patch Changes
@@ -1,22 +1,18 @@
1
1
  'use strict';
2
2
 
3
+ var axios = require('axios');
4
+
3
5
  class FetchError extends Error {
4
6
  }
5
7
  async function request(url, options) {
6
8
  try {
7
- const res = await fetch(url, {
9
+ const res = await axios({
10
+ url,
8
11
  headers: options?.headers,
9
12
  method: options?.method,
10
- signal: AbortSignal.timeout(options?.timeout ?? 3e4)
13
+ timeout: options?.timeout ?? 3e4
11
14
  });
12
- if (!res.ok) {
13
- throw new FetchError(res.statusText);
14
- }
15
- const contentType = res.headers.get("content-type");
16
- if (contentType?.includes("application/json")) {
17
- return { data: await res.json() };
18
- }
19
- return { data: await res.text() };
15
+ return { data: res.data };
20
16
  } catch (error) {
21
17
  throw new FetchError(
22
18
  error instanceof Error ? error.message : String(error)
@@ -24,12 +20,31 @@ async function request(url, options) {
24
20
  }
25
21
  }
26
22
 
23
+ function pingDebounceFactory(apiKey, config) {
24
+ return async function pingDebounce() {
25
+ await request(
26
+ `https://api.debounce.io/v1/balance?api=${apiKey}`,
27
+ {
28
+ method: "get",
29
+ headers: {
30
+ accept: "application/json"
31
+ },
32
+ timeout: config?.timeout
33
+ }
34
+ );
35
+ return true;
36
+ };
37
+ }
38
+ function mockPingDebounce() {
39
+ return Promise.resolve(true);
40
+ }
41
+
27
42
  function singleValidationFactory(apiKey, config) {
28
43
  return async function singleValidation(email) {
29
44
  const {
30
45
  data: { debounce }
31
46
  } = await request(
32
- `https://api.debounce.io/v1/?email=${email}&api=${apiKey}`,
47
+ `https://api.debounce.io/v1/?email=${encodeURIComponent(email)}&api=${encodeURIComponent(apiKey)}`,
33
48
  {
34
49
  method: "get",
35
50
  headers: {
@@ -41,5 +56,42 @@ function singleValidationFactory(apiKey, config) {
41
56
  return debounce;
42
57
  };
43
58
  }
59
+ function mockSingleValidation(email) {
60
+ const localPart = email.split("@")[0];
61
+ const domain = email.split("@")[1];
62
+ if (domain === "invalid-domain.com") {
63
+ return Promise.resolve({
64
+ send_transactional: "0",
65
+ did_you_mean: "",
66
+ code: "6"
67
+ });
68
+ }
69
+ if (domain === "ypomail.com") {
70
+ return Promise.resolve({
71
+ send_transactional: "0",
72
+ did_you_mean: `${localPart}@yopmail.com`,
73
+ code: "6"
74
+ });
75
+ }
76
+ return Promise.resolve({
77
+ send_transactional: "1",
78
+ did_you_mean: void 0,
79
+ code: "200"
80
+ });
81
+ }
82
+
83
+ const mockDebounceClientFactory = () => {
84
+ return {
85
+ singleValidation: mockSingleValidation,
86
+ ping: mockPingDebounce
87
+ };
88
+ };
89
+ const debounceClientFactory = (apiKey, config) => {
90
+ return {
91
+ singleValidation: singleValidationFactory(apiKey, config),
92
+ ping: pingDebounceFactory(apiKey, config)
93
+ };
94
+ };
44
95
 
45
- exports.singleValidationFactory = singleValidationFactory;
96
+ exports.debounceClientFactory = debounceClientFactory;
97
+ exports.mockDebounceClientFactory = mockDebounceClientFactory;
@@ -1,25 +1,39 @@
1
+ import { DebounceSingleValidationSuccessResponse } from '#src/types';
2
+
3
+ declare function mockPingDebounce(): Promise<boolean>;
4
+
1
5
  /**
2
- * Perform a single email validation request.
6
+ * Mock single email validation response.
3
7
  *
4
- * @see https://developers.debounce.io/reference/single-validation#response-parameters
8
+ * @see https://developers.debounce.com/api-reference/endpoint/single-validation#single-email-validation
5
9
  * @param apiKey the debounce.io API key
6
10
  * @param config the Axios request config
7
11
  * @returns Debounce Single Validation response
8
12
  */
9
- declare function singleValidationFactory(apiKey: string, config?: {
13
+ declare function mockSingleValidation(email: string): Promise<DebounceSingleValidationSuccessResponse["debounce"]>;
14
+
15
+ declare const mockDebounceClientFactory: () => {
16
+ singleValidation: typeof mockSingleValidation;
17
+ ping: typeof mockPingDebounce;
18
+ };
19
+ declare const debounceClientFactory: (apiKey: string, config?: {
10
20
  timeout?: number;
11
- }): (email: string) => Promise<{
12
- email: string;
13
- code: string;
14
- role: string;
15
- free_email: string;
16
- result: string;
17
- reason: string;
18
- send_transactional: string;
19
- did_you_mean: string;
20
- }>;
21
- type SingleValidationHandler = ReturnType<typeof singleValidationFactory>;
21
+ }) => {
22
+ singleValidation: (email: string) => Promise<{
23
+ email: string;
24
+ code: string;
25
+ role: string;
26
+ free_email: string;
27
+ result: string;
28
+ reason: string;
29
+ send_transactional: string;
30
+ did_you_mean: string;
31
+ }>;
32
+ ping: () => Promise<boolean>;
33
+ };
34
+
35
+ type DebounceClientType = ReturnType<typeof debounceClientFactory>;
22
36
 
23
- export { singleValidationFactory };
24
- export type { SingleValidationHandler };
37
+ export { debounceClientFactory, mockDebounceClientFactory };
38
+ export type { DebounceClientType };
25
39
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sources":["../../src/api/single-validation.ts"],"mappings":"AAOA;AANA;AACA;AACA;AACA;AACA;AACA;GAQG;iBACa,uBAAuB,CACrC,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE;IAAE,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,IAEU,OAAO,MAAM;AARtD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;GAiBC;KAEW,uBAAuB,GAAG,UAAU,CAC9C,OAAO,uBAAuB,CAC/B,CAAC;;;;","names":[]}
1
+ {"version":3,"file":"index.d.ts","sources":["../../src/api/ping.ts","../../src/api/single-validation.ts","../../src/api/index.ts"],"mappings":";;AAyBA,iBAAgB,gBAAgB;;ACYhC;;;;;;;;AASA,iBAAgB,oBAAoB,iBAEjC,OAAO,CAAC,uCAAuC;;ACxClD,cAAM,yBAAyB;;;;AAO/B,cAAM,qBAAqB;;;;;;;;;;;;;;;;AAWrB,KAAM,kBAAkB,GAAG,UAAU,QAAQ,qBAAqB","names":[]}
package/dist/api/index.js CHANGED
@@ -1,20 +1,16 @@
1
+ import axios from 'axios';
2
+
1
3
  class FetchError extends Error {
2
4
  }
3
5
  async function request(url, options) {
4
6
  try {
5
- const res = await fetch(url, {
7
+ const res = await axios({
8
+ url,
6
9
  headers: options?.headers,
7
10
  method: options?.method,
8
- signal: AbortSignal.timeout(options?.timeout ?? 3e4)
11
+ timeout: options?.timeout ?? 3e4
9
12
  });
10
- if (!res.ok) {
11
- throw new FetchError(res.statusText);
12
- }
13
- const contentType = res.headers.get("content-type");
14
- if (contentType?.includes("application/json")) {
15
- return { data: await res.json() };
16
- }
17
- return { data: await res.text() };
13
+ return { data: res.data };
18
14
  } catch (error) {
19
15
  throw new FetchError(
20
16
  error instanceof Error ? error.message : String(error)
@@ -22,12 +18,31 @@ async function request(url, options) {
22
18
  }
23
19
  }
24
20
 
21
+ function pingDebounceFactory(apiKey, config) {
22
+ return async function pingDebounce() {
23
+ await request(
24
+ `https://api.debounce.io/v1/balance?api=${apiKey}`,
25
+ {
26
+ method: "get",
27
+ headers: {
28
+ accept: "application/json"
29
+ },
30
+ timeout: config?.timeout
31
+ }
32
+ );
33
+ return true;
34
+ };
35
+ }
36
+ function mockPingDebounce() {
37
+ return Promise.resolve(true);
38
+ }
39
+
25
40
  function singleValidationFactory(apiKey, config) {
26
41
  return async function singleValidation(email) {
27
42
  const {
28
43
  data: { debounce }
29
44
  } = await request(
30
- `https://api.debounce.io/v1/?email=${email}&api=${apiKey}`,
45
+ `https://api.debounce.io/v1/?email=${encodeURIComponent(email)}&api=${encodeURIComponent(apiKey)}`,
31
46
  {
32
47
  method: "get",
33
48
  headers: {
@@ -39,5 +54,41 @@ function singleValidationFactory(apiKey, config) {
39
54
  return debounce;
40
55
  };
41
56
  }
57
+ function mockSingleValidation(email) {
58
+ const localPart = email.split("@")[0];
59
+ const domain = email.split("@")[1];
60
+ if (domain === "invalid-domain.com") {
61
+ return Promise.resolve({
62
+ send_transactional: "0",
63
+ did_you_mean: "",
64
+ code: "6"
65
+ });
66
+ }
67
+ if (domain === "ypomail.com") {
68
+ return Promise.resolve({
69
+ send_transactional: "0",
70
+ did_you_mean: `${localPart}@yopmail.com`,
71
+ code: "6"
72
+ });
73
+ }
74
+ return Promise.resolve({
75
+ send_transactional: "1",
76
+ did_you_mean: void 0,
77
+ code: "200"
78
+ });
79
+ }
80
+
81
+ const mockDebounceClientFactory = () => {
82
+ return {
83
+ singleValidation: mockSingleValidation,
84
+ ping: mockPingDebounce
85
+ };
86
+ };
87
+ const debounceClientFactory = (apiKey, config) => {
88
+ return {
89
+ singleValidation: singleValidationFactory(apiKey, config),
90
+ ping: pingDebounceFactory(apiKey, config)
91
+ };
92
+ };
42
93
 
43
- export { singleValidationFactory };
94
+ export { debounceClientFactory, mockDebounceClientFactory };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@proconnect-gouv/proconnect.debounce",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "homepage": "https://github.com/proconnect-gouv/proconnect-identite/tree/main/packages/debounce#readme",
5
5
  "bugs": "https://github.com/proconnect-gouv/proconnect-identite/issues",
6
6
  "repository": {
@@ -36,7 +36,8 @@
36
36
  "test": "tsx --test --test-reporter=spec src/**/*.test.ts"
37
37
  },
38
38
  "dependencies": {
39
- "zod": "^4.3.5"
39
+ "axios": "^1.21.1",
40
+ "zod": "^4.6.7"
40
41
  },
41
42
  "devDependencies": {
42
43
  "@proconnect-gouv/proconnect.devtools.typescript": "1.0.0",
package/src/api/index.ts CHANGED
@@ -1,3 +1,27 @@
1
1
  //
2
2
 
3
- export * from "./single-validation.js";
3
+ import { mockPingDebounce, pingDebounceFactory } from "./ping.js";
4
+ import {
5
+ mockSingleValidation,
6
+ singleValidationFactory,
7
+ } from "./single-validation.js";
8
+
9
+ const mockDebounceClientFactory = () => {
10
+ return {
11
+ singleValidation: mockSingleValidation,
12
+ ping: mockPingDebounce,
13
+ };
14
+ };
15
+
16
+ const debounceClientFactory = (
17
+ apiKey: string,
18
+ config?: { timeout?: number },
19
+ ) => {
20
+ return {
21
+ singleValidation: singleValidationFactory(apiKey, config),
22
+ ping: pingDebounceFactory(apiKey, config),
23
+ };
24
+ };
25
+
26
+ export { debounceClientFactory, mockDebounceClientFactory };
27
+ export type DebounceClientType = ReturnType<typeof debounceClientFactory>;
@@ -0,0 +1,28 @@
1
+ //
2
+
3
+ import type { DebounceBalanceSuccessResponse } from "#src/types";
4
+ import { request } from "./request.js";
5
+
6
+ export function pingDebounceFactory(
7
+ apiKey: string,
8
+ config?: { timeout?: number },
9
+ ) {
10
+ return async function pingDebounce() {
11
+ await request<DebounceBalanceSuccessResponse>(
12
+ `https://api.debounce.io/v1/balance?api=${apiKey}`,
13
+ {
14
+ method: "get",
15
+ headers: {
16
+ accept: "application/json",
17
+ },
18
+ timeout: config?.timeout,
19
+ },
20
+ );
21
+
22
+ return true;
23
+ };
24
+ }
25
+
26
+ export function mockPingDebounce() {
27
+ return Promise.resolve(true);
28
+ }
@@ -1,3 +1,5 @@
1
+ import axios from "axios";
2
+
1
3
  class FetchError extends Error {}
2
4
 
3
5
  async function request<responseT>(
@@ -9,19 +11,13 @@ async function request<responseT>(
9
11
  },
10
12
  ): Promise<{ data: responseT }> {
11
13
  try {
12
- const res = await fetch(url, {
14
+ const res = await axios({
15
+ url,
13
16
  headers: options?.headers,
14
17
  method: options?.method,
15
- signal: AbortSignal.timeout(options?.timeout ?? 30_000),
18
+ timeout: options?.timeout ?? 30000,
16
19
  });
17
- if (!res.ok) {
18
- throw new FetchError(res.statusText);
19
- }
20
- const contentType = res.headers.get("content-type");
21
- if (contentType?.includes("application/json")) {
22
- return { data: (await res.json()) as responseT };
23
- }
24
- return { data: (await res.text()) as unknown as responseT };
20
+ return { data: res.data as responseT };
25
21
  } catch (error) {
26
22
  throw new FetchError(
27
23
  error instanceof Error ? error.message : String(error),
@@ -1,6 +1,6 @@
1
1
  //
2
2
 
3
- import type { DebounceSuccessResponse } from "#src/types";
3
+ import type { DebounceSingleValidationSuccessResponse } from "#src/types";
4
4
  import { request } from "./request.js";
5
5
 
6
6
  //
@@ -8,7 +8,7 @@ import { request } from "./request.js";
8
8
  /**
9
9
  * Perform a single email validation request.
10
10
  *
11
- * @see https://developers.debounce.io/reference/single-validation#response-parameters
11
+ * @see https://developers.debounce.com/api-reference/endpoint/single-validation#single-email-validation
12
12
  * @param apiKey the debounce.io API key
13
13
  * @param config the Axios request config
14
14
  * @returns Debounce Single Validation response
@@ -20,8 +20,8 @@ export function singleValidationFactory(
20
20
  return async function singleValidation(email: string) {
21
21
  const {
22
22
  data: { debounce },
23
- } = await request<DebounceSuccessResponse>(
24
- `https://api.debounce.io/v1/?email=${email}&api=${apiKey}`,
23
+ } = await request<DebounceSingleValidationSuccessResponse>(
24
+ `https://api.debounce.io/v1/?email=${encodeURIComponent(email)}&api=${encodeURIComponent(apiKey)}`,
25
25
  {
26
26
  method: "get",
27
27
  headers: {
@@ -35,6 +35,41 @@ export function singleValidationFactory(
35
35
  };
36
36
  }
37
37
 
38
+ /**
39
+ * Mock single email validation response.
40
+ *
41
+ * @see https://developers.debounce.com/api-reference/endpoint/single-validation#single-email-validation
42
+ * @param apiKey the debounce.io API key
43
+ * @param config the Axios request config
44
+ * @returns Debounce Single Validation response
45
+ */
46
+
47
+ export function mockSingleValidation(
48
+ email: string,
49
+ ): Promise<DebounceSingleValidationSuccessResponse["debounce"]> {
50
+ const localPart = email.split("@")[0];
51
+ const domain = email.split("@")[1];
52
+ if (domain === "invalid-domain.com") {
53
+ return Promise.resolve({
54
+ send_transactional: "0",
55
+ did_you_mean: "",
56
+ code: "6",
57
+ }) as any;
58
+ }
59
+ if (domain === "ypomail.com") {
60
+ return Promise.resolve({
61
+ send_transactional: "0",
62
+ did_you_mean: `${localPart}@yopmail.com`,
63
+ code: "6",
64
+ }) as any;
65
+ }
66
+ return Promise.resolve({
67
+ send_transactional: "1",
68
+ did_you_mean: undefined,
69
+ code: "200",
70
+ }) as any;
71
+ }
72
+
38
73
  export type SingleValidationHandler = ReturnType<
39
74
  typeof singleValidationFactory
40
75
  >;
@@ -14,7 +14,7 @@ export interface DebounceErrorResponse {
14
14
  /**
15
15
  * @see https://developers.debounce.io/reference/responses#success-response
16
16
  */
17
- export interface DebounceSuccessResponse {
17
+ export interface DebounceSingleValidationSuccessResponse {
18
18
  debounce: {
19
19
  // The email address you are requesting to validate.
20
20
  // ex: 'test@wanadoo.rf'
@@ -45,3 +45,7 @@ export interface DebounceSuccessResponse {
45
45
  success: "1";
46
46
  balance: `${number}`;
47
47
  }
48
+
49
+ export interface DebounceBalanceSuccessResponse {
50
+ balance: `${number}`;
51
+ }