sgp-sdk 0.0.0-stage → 1.2.0
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/LICENSE +21 -0
- package/README.md +303 -2
- package/dist/client.d.ts +228 -0
- package/dist/endpoint.d.ts +66 -0
- package/dist/endpoints/crm/address.d.ts +20 -0
- package/dist/endpoints/crm/create-contract.d.ts +109 -0
- package/dist/endpoints/crm/create-customer.d.ts +65 -0
- package/dist/endpoints/crm/update-customer-status.d.ts +43 -0
- package/dist/endpoints/os/list-occurrence-reasons.d.ts +24 -0
- package/dist/endpoints/os/list-occurrence-types.d.ts +25 -0
- package/dist/endpoints/precadastro/list-due-days.d.ts +19 -0
- package/dist/endpoints/precadastro/list-sellers.d.ts +21 -0
- package/dist/endpoints/suporte/add-customer-document.d.ts +24 -0
- package/dist/endpoints/suporte/list-customer-documents.d.ts +25 -0
- package/dist/endpoints/ura/check-coverage.d.ts +46 -0
- package/dist/endpoints/ura/list-annotations.d.ts +32 -0
- package/dist/endpoints/ura/list-clients.d.ts +58 -0
- package/dist/endpoints/ura/list-contracts.d.ts +45 -0
- package/dist/endpoints/ura/list-invoices.d.ts +107 -0
- package/dist/endpoints/ura/list-nas.d.ts +29 -0
- package/dist/endpoints/ura/list-occurrence-methods.d.ts +19 -0
- package/dist/endpoints/ura/list-payment-carriers.d.ts +20 -0
- package/dist/endpoints/ura/list-plans.d.ts +70 -0
- package/dist/endpoints/ura/list-pops.d.ts +19 -0
- package/dist/endpoints/ura/list-service-orders.d.ts +64 -0
- package/dist/endpoints/ura/lookup-customer.d.ts +121 -0
- package/dist/endpoints/ura/open-ticket.d.ts +81 -0
- package/dist/errors.d.ts +59 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +1449 -0
- package/dist/result.d.ts +55 -0
- package/dist/schema/parse.d.ts +19 -0
- package/dist/schema/paths.d.ts +38 -0
- package/dist/sensitivity.d.ts +7 -0
- package/dist/transport.d.ts +65 -0
- package/package.json +34 -4
package/dist/result.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** Normalized failure category returned by every SDK method. */
|
|
2
|
+
export type SgpErrorCode = "UNAUTHORIZED" | "ROUTE_NOT_ALLOWED" | "NOT_FOUND" | "METHOD_NOT_ALLOWED" | "VALIDATION" | "SCHEMA_MISMATCH" | "UPSTREAM" | "TIMEOUT" | "NETWORK" | "UNKNOWN";
|
|
3
|
+
/** Normalized error. Never carries credentials, query strings, raw bodies or fetch exception messages. */
|
|
4
|
+
export interface SgpError {
|
|
5
|
+
/** HTTP status, or 0 when no response was received. */
|
|
6
|
+
status: number;
|
|
7
|
+
code: SgpErrorCode;
|
|
8
|
+
/** Written by the SDK, in English. */
|
|
9
|
+
message: string;
|
|
10
|
+
/**
|
|
11
|
+
* Text from SGP's error body, copied verbatim (it can echo values from the request; the SDK adds
|
|
12
|
+
* nothing to it). Precedence: `detail` string, then non-empty `msg` string, then `errors`
|
|
13
|
+
* (array items, or `"key: value"` pairs from an object, joined by `"; "`), then non-empty
|
|
14
|
+
* `error` string. 401/403 are classified by it.
|
|
15
|
+
*/
|
|
16
|
+
detail?: string;
|
|
17
|
+
/** Request path only, without query string. */
|
|
18
|
+
endpoint: string;
|
|
19
|
+
}
|
|
20
|
+
/** A non-key field whose type did not match the schema; reports type names, never values. */
|
|
21
|
+
export interface SchemaWarning {
|
|
22
|
+
path: string;
|
|
23
|
+
expected: string;
|
|
24
|
+
received: string;
|
|
25
|
+
}
|
|
26
|
+
/** Outcome of every SDK method; methods never throw for network, HTTP or schema failures. */
|
|
27
|
+
export type SgpResult<T> = {
|
|
28
|
+
success: true;
|
|
29
|
+
data: T;
|
|
30
|
+
warnings?: SchemaWarning[];
|
|
31
|
+
} | {
|
|
32
|
+
success: false;
|
|
33
|
+
error: SgpError;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Whether SGP applied a failed write: `"not_applied"` for a 4xx or a request rejected locally before
|
|
37
|
+
* being sent (`VALIDATION`, status 0), `"unknown"` for everything else. The exception is SGP's
|
|
38
|
+
* generic exception wrapper, a 4xx (in practice 400) whose body `errors` is an array (e.g.
|
|
39
|
+
* `"Ocorreu um erro: ..."`): SGP may have failed after applying the write, so it is `"unknown"`.
|
|
40
|
+
* 401/403/404/405/422 stay `"not_applied"`.
|
|
41
|
+
*/
|
|
42
|
+
export type WriteOutcome = "not_applied" | "unknown";
|
|
43
|
+
/** Normalized write error; `outcome` says whether the write may have taken effect. */
|
|
44
|
+
export interface SgpWriteError extends SgpError {
|
|
45
|
+
outcome: WriteOutcome;
|
|
46
|
+
}
|
|
47
|
+
/** Outcome of every SDK write method; like reads, they never throw for network, HTTP or schema failures. */
|
|
48
|
+
export type SgpWriteResult<T> = {
|
|
49
|
+
success: true;
|
|
50
|
+
data: T;
|
|
51
|
+
warnings?: SchemaWarning[];
|
|
52
|
+
} | {
|
|
53
|
+
success: false;
|
|
54
|
+
error: SgpWriteError;
|
|
55
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import type { SgpResult } from "../result.js";
|
|
3
|
+
/**
|
|
4
|
+
* Validates an SGP response body, tolerating type drift in non-key fields.
|
|
5
|
+
*
|
|
6
|
+
* 1. Parses with `schema` (unknown keys are stripped at every level). Success → `data`.
|
|
7
|
+
* 2. Otherwise picks, per issue, the property to remove: the property the issue points at or, when
|
|
8
|
+
* the issue sits on an array element, the nearest enclosing object property (the whole array
|
|
9
|
+
* field, e.g. `clientes[0].contatos.telefones`). If none exists, or it is a key field or an
|
|
10
|
+
* ancestor of one (`keyFields`, normalized paths like `clientes[].id`) → `SCHEMA_MISMATCH`.
|
|
11
|
+
* That property is the issue's own path or a prefix of it, so every issue on a key field or on
|
|
12
|
+
* an ancestor of one ends here.
|
|
13
|
+
* 3. Removes those properties from a clone of `input` and parses exactly once more. Success →
|
|
14
|
+
* `data` plus one `SchemaWarning` per issue, with the precise issue location
|
|
15
|
+
* (e.g. `clientes[0].contatos.telefones[0]`) and type names only. Failure → `SCHEMA_MISMATCH`.
|
|
16
|
+
*
|
|
17
|
+
* `SCHEMA_MISMATCH.detail` lists the failing normalized paths (`(root)` for the body itself), never values.
|
|
18
|
+
*/
|
|
19
|
+
export declare const parseResponse: <S extends z.ZodType>(schema: S, keyFields: readonly string[], input: unknown, endpoint: string) => SgpResult<z.output<S>>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Joins a Zod issue path with array indices collapsed to `[]`, the form used by `keyFields`.
|
|
3
|
+
*
|
|
4
|
+
* `["contratos", 0, "contratoId"]` → `"contratos[].contratoId"`; `[0, "id"]` → `"[].id"`; `[]` → `""`.
|
|
5
|
+
*/
|
|
6
|
+
export declare const normalizePath: (path: ReadonlyArray<PropertyKey>) => string;
|
|
7
|
+
/**
|
|
8
|
+
* Joins a Zod issue path keeping the real array indices, the form used in `SchemaWarning.path`.
|
|
9
|
+
*
|
|
10
|
+
* `["clientes", 0, "sexo"]` → `"clientes[0].sexo"`.
|
|
11
|
+
*/
|
|
12
|
+
export declare const formatPath: (path: ReadonlyArray<PropertyKey>) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Tells whether a raw Zod issue path is a key field or an ancestor of one.
|
|
15
|
+
*
|
|
16
|
+
* Segments are compared as they are, never joined into a string, so a data-defined property name
|
|
17
|
+
* holding `.`, `[` or `]` stays one segment. The root (`[]`) is an ancestor of every key.
|
|
18
|
+
* A numeric segment matches only a `[]` key segment. A string segment matches a `*` key segment
|
|
19
|
+
* (any name, the empty one included) or an equal name, never `[]`: `["[].*"]` covers `[0, "Geogrid"]`
|
|
20
|
+
* and `[0, "Geo.grid"]` (data-defined names), but not what sits below them.
|
|
21
|
+
*/
|
|
22
|
+
export declare const isKeyPathOf: (path: ReadonlyArray<PropertyKey>, keyFields: readonly string[]) => boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Tells whether a normalized path is a key field or an ancestor of one.
|
|
25
|
+
*
|
|
26
|
+
* The root (`""`) is an ancestor of every key. `"cliente"` is not an ancestor of `"clientes[].id"`.
|
|
27
|
+
* A `*` segment in a key matches exactly one property name, never an array index: `"[].*"` covers
|
|
28
|
+
* `"[].SGPMaps"` and `"[].Geogrid"` (data-defined names), but not what sits below them.
|
|
29
|
+
*/
|
|
30
|
+
export declare const isKeyPath: (path: string, keyFields: readonly string[]) => boolean;
|
|
31
|
+
/** Reads the value at `path`, or `undefined` when any step is missing or not indexable. */
|
|
32
|
+
export declare const valueAt: (input: unknown, path: ReadonlyArray<PropertyKey>) => unknown;
|
|
33
|
+
/**
|
|
34
|
+
* Names the type of the value at `path`; never returns the value itself.
|
|
35
|
+
*
|
|
36
|
+
* One of `"string" | "number" | "boolean" | "null" | "undefined" | "array" | "object"` for JSON input.
|
|
37
|
+
*/
|
|
38
|
+
export declare const typeNameAt: (input: unknown, path: ReadonlyArray<PropertyKey>) => string;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a shallow copy of `obj` without `keys`, typed accordingly.
|
|
3
|
+
*
|
|
4
|
+
* Meant for the `secretKeys` / `personalKeys` tuples of an endpoint, so callers can drop sensitive
|
|
5
|
+
* fields before logging or forwarding an item. Does not mutate `obj`.
|
|
6
|
+
*/
|
|
7
|
+
export declare const omitSensitive: <T extends object, K extends keyof T>(obj: T, keys: readonly K[]) => Omit<T, K>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { SgpError } from "./result.js";
|
|
2
|
+
/** Default request timeout in milliseconds. */
|
|
3
|
+
export declare const DEFAULT_TIMEOUT_MS = 20000;
|
|
4
|
+
/** Connection settings shared by every SDK method. */
|
|
5
|
+
export interface SgpClientConfig {
|
|
6
|
+
baseUrl: string;
|
|
7
|
+
token: string;
|
|
8
|
+
app: string;
|
|
9
|
+
/** Request timeout in milliseconds. Defaults to {@link DEFAULT_TIMEOUT_MS}. */
|
|
10
|
+
timeoutMs?: number;
|
|
11
|
+
/** Custom `fetch`, mainly for tests. Defaults to the global `fetch`. */
|
|
12
|
+
fetch?: (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A request parameter value. Arrays and objects are only valid on POST/PUT; `undefined` object keys
|
|
16
|
+
* are omitted at any depth.
|
|
17
|
+
*/
|
|
18
|
+
export type ParamValue = string | number | boolean | null | readonly ParamValue[] | {
|
|
19
|
+
readonly [key: string]: ParamValue | undefined;
|
|
20
|
+
};
|
|
21
|
+
/** A file sent as one part of a multipart request. */
|
|
22
|
+
export interface SgpFile {
|
|
23
|
+
data: Blob | Uint8Array | ArrayBuffer;
|
|
24
|
+
filename: string;
|
|
25
|
+
/** MIME type of the part. Defaults to the `type` of a `Blob`; none for bytes. */
|
|
26
|
+
contentType?: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* One SGP request; `path` starts with `/` and may hold `{name}` placeholders filled from
|
|
30
|
+
* `pathParams`. `params` with `undefined` values are dropped. POST/PUT with `files` is sent as
|
|
31
|
+
* multipart; otherwise as JSON.
|
|
32
|
+
*/
|
|
33
|
+
export interface RequestSpec {
|
|
34
|
+
method: "GET" | "POST" | "PUT";
|
|
35
|
+
path: string;
|
|
36
|
+
pathParams?: Record<string, string | number>;
|
|
37
|
+
params?: Record<string, ParamValue | undefined>;
|
|
38
|
+
files?: Record<string, SgpFile>;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Parsed JSON body of a 2xx response with its HTTP status, or the normalized failure.
|
|
42
|
+
* `hasErrorList` is set on a non-2xx answer: its JSON body is SGP's generic exception wrapper
|
|
43
|
+
* (`errors` is an array). The body itself is never kept.
|
|
44
|
+
*/
|
|
45
|
+
export type RawResponse = {
|
|
46
|
+
ok: true;
|
|
47
|
+
body: unknown;
|
|
48
|
+
status: number;
|
|
49
|
+
} | {
|
|
50
|
+
ok: false;
|
|
51
|
+
error: SgpError;
|
|
52
|
+
hasErrorList?: boolean;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Sends one request to SGP and returns the parsed JSON body or a normalized error.
|
|
56
|
+
*
|
|
57
|
+
* GET sends `token` and `app` in the query string and only accepts primitive params; POST/PUT send
|
|
58
|
+
* them in a JSON body, or as multipart fields when `files` is set. A 2xx response with an empty
|
|
59
|
+
* body yields `body: null`. The success result carries the real 2xx `status`. Redirects are never
|
|
60
|
+
* followed (`redirect: "manual"`): a 3xx comes back as an `UNKNOWN` error with its status.
|
|
61
|
+
* Never throws: invalid path params or structured GET params (`VALIDATION`, status 0, no request
|
|
62
|
+
* sent), network failures, timeouts, non-2xx statuses and non-JSON 2xx bodies all come back as
|
|
63
|
+
* `{ ok: false, error }`, where `error.endpoint` is `spec.path` (the template) only.
|
|
64
|
+
*/
|
|
65
|
+
export declare const sendRequest: (config: SgpClientConfig, spec: RequestSpec) => Promise<RawResponse>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sgp-sdk",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "Unofficial TypeScript client for the SGP (TSMX) ISP ERP HTTP API, with normalized errors and explicit write outcomes. Not affiliated with TSMX.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=22"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "rm -rf dist && bun build src/index.ts --outdir dist --target node --format esm --external zod && tsc -p tsconfig.build.json",
|
|
22
|
+
"release": "bun scripts/release.ts",
|
|
23
|
+
"test": "bun test",
|
|
24
|
+
"lint": "biome check .",
|
|
25
|
+
"typecheck": "tsc --noEmit",
|
|
26
|
+
"check:consumer-types": "bun run build && tsc -p tests/consumer-types/tsconfig.json"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"zod": "^4"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@biomejs/biome": "^2.6.4",
|
|
33
|
+
"@types/bun": "^1.4.4",
|
|
34
|
+
"typescript": "^7.0.2"
|
|
35
|
+
}
|
|
36
|
+
}
|