square-sdk-cli 0.1.1 → 0.1.2
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/README.md +11 -2
- package/dist/square.js +59 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -55,11 +55,20 @@ square payments --help
|
|
|
55
55
|
square terminal actions create --help
|
|
56
56
|
|
|
57
57
|
# Call methods
|
|
58
|
-
square
|
|
58
|
+
square locations list
|
|
59
|
+
square catalog list
|
|
60
|
+
square payments list --sortField CREATED_AT
|
|
61
|
+
square customers list --sortField DEFAULT --sortOrder ASC
|
|
59
62
|
square payments create --json '{"sourceId":"...","idempotencyKey":"..."}' --amountMoney.amount 100 --amountMoney.currency USD
|
|
60
|
-
square customers list --stream
|
|
63
|
+
square customers list --sortField DEFAULT --sortOrder ASC --stream
|
|
61
64
|
```
|
|
62
65
|
|
|
66
|
+
> Note: some `list` endpoints (e.g. `payments`, `customers`) require explicit
|
|
67
|
+
> sort flags — the Square SDK serializes a missing sort field as an empty string
|
|
68
|
+
> and the API rejects it with `INVALID_ENUM_VALUE ... sort_field`. Pass
|
|
69
|
+
> `--sortField` (and `--sortOrder` for `customers`) as shown above. Endpoints
|
|
70
|
+
> like `locations` and `catalog` need no arguments.
|
|
71
|
+
|
|
63
72
|
### Request body
|
|
64
73
|
|
|
65
74
|
- `--json '<json>'` — full request body as JSON
|
package/dist/square.js
CHANGED
|
@@ -377,19 +377,72 @@ function print(value, options = {}) {
|
|
|
377
377
|
write(JSON.stringify(value, bigIntReplacer, 2));
|
|
378
378
|
}
|
|
379
379
|
}
|
|
380
|
+
var STATUS_REASON = {
|
|
381
|
+
400: "Bad Request",
|
|
382
|
+
401: "Unauthorized",
|
|
383
|
+
403: "Forbidden",
|
|
384
|
+
404: "Not Found",
|
|
385
|
+
405: "Method Not Allowed",
|
|
386
|
+
409: "Conflict",
|
|
387
|
+
422: "Unprocessable Entity",
|
|
388
|
+
429: "Too Many Requests",
|
|
389
|
+
500: "Internal Server Error",
|
|
390
|
+
503: "Service Unavailable"
|
|
391
|
+
};
|
|
392
|
+
function hintFor(e) {
|
|
393
|
+
const field = e.field ?? "";
|
|
394
|
+
if (e.code === "INVALID_ENUM_VALUE" && /^sort_/.test(field)) {
|
|
395
|
+
if (field === "sort_field")
|
|
396
|
+
return "This list endpoint needs an explicit sort field, e.g. `--sortField CREATED_AT`.";
|
|
397
|
+
if (field === "sort_order")
|
|
398
|
+
return "This list endpoint needs an explicit sort order, e.g. `--sortOrder ASC`.";
|
|
399
|
+
}
|
|
400
|
+
switch (e.code) {
|
|
401
|
+
case "UNAUTHORIZED":
|
|
402
|
+
case "ACCESS_TOKEN_EXPIRED":
|
|
403
|
+
case "ACCESS_TOKEN_REVOKED":
|
|
404
|
+
case "AUTHENTICATION_ERROR":
|
|
405
|
+
return "Check your access token (`square config set <profile> --token ...` or $SQUARE_ACCESS_TOKEN).";
|
|
406
|
+
case "FORBIDDEN":
|
|
407
|
+
case "INSUFFICIENT_SCOPES":
|
|
408
|
+
return "Your token lacks the OAuth scope/permission for this endpoint.";
|
|
409
|
+
case "NOT_FOUND":
|
|
410
|
+
return "The referenced resource does not exist in this environment (sandbox vs production?).";
|
|
411
|
+
case "RATE_LIMITED":
|
|
412
|
+
return "You hit the API rate limit — retry after a short delay.";
|
|
413
|
+
case "VALUE_EMPTY":
|
|
414
|
+
case "MISSING_REQUIRED_PARAMETER":
|
|
415
|
+
return e.field ? `Provide the required field: \`--${camelize(e.field)} <value>\` or via \`--json\`.` : "A required field is missing — pass it with `--<field> <value>` or `--json`.";
|
|
416
|
+
default:
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
function camelize(field) {
|
|
421
|
+
return field.split(".").map((seg) => seg.replace(/_([a-z])/g, (_, c) => c.toUpperCase())).join(".");
|
|
422
|
+
}
|
|
380
423
|
function renderError(err, options = {}) {
|
|
381
424
|
const write = options.writeFn ?? ((msg) => process.stderr.write(msg + `
|
|
382
425
|
`));
|
|
383
426
|
if (err instanceof SquareError) {
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
427
|
+
const status = err.statusCode;
|
|
428
|
+
const reason = status ? STATUS_REASON[status] : undefined;
|
|
429
|
+
const statusText = status ? `HTTP ${status}${reason ? " " + reason : ""}` : "request failed";
|
|
430
|
+
write(`✗ Square API request failed (${statusText})`);
|
|
431
|
+
const errors = err.errors ?? [];
|
|
432
|
+
for (const e of errors) {
|
|
433
|
+
write("");
|
|
434
|
+
write(` • ${e.detail ?? e.code}`);
|
|
435
|
+
if (e.field)
|
|
436
|
+
write(` field: ${e.field}`);
|
|
437
|
+
write(` code: ${e.category} / ${e.code}`);
|
|
438
|
+
const hint = hintFor(e);
|
|
439
|
+
if (hint)
|
|
440
|
+
write(` hint: ${hint}`);
|
|
388
441
|
}
|
|
389
442
|
} else if (err instanceof Error) {
|
|
390
|
-
write(
|
|
443
|
+
write(`✗ Error: ${err.message}`);
|
|
391
444
|
} else {
|
|
392
|
-
write(
|
|
445
|
+
write(`✗ Error: ${String(err)}`);
|
|
393
446
|
}
|
|
394
447
|
return 1;
|
|
395
448
|
}
|