@brigadasos/nadeshiko-sdk 1.4.0-dev.2819d72 → 1.4.0-dev.7a7fd76
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 +99 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,14 +19,14 @@ bun add @brigadasos/nadeshiko-sdk@internal
|
|
|
19
19
|
The client sends your API key as `Authorization: Bearer <apiKey>`.
|
|
20
20
|
|
|
21
21
|
```typescript
|
|
22
|
-
import { createClient,
|
|
22
|
+
import { createClient, searchSegments } from '@brigadasos/nadeshiko-sdk';
|
|
23
23
|
|
|
24
24
|
const client = createClient({
|
|
25
25
|
apiKey: process.env.NADESHIKO_API_KEY!,
|
|
26
26
|
baseUrl: 'PRODUCTION',
|
|
27
27
|
});
|
|
28
28
|
|
|
29
|
-
const result = await
|
|
29
|
+
const result = await searchSegments({
|
|
30
30
|
client,
|
|
31
31
|
body: { query: '彼女' },
|
|
32
32
|
});
|
|
@@ -37,3 +37,100 @@ if (result.error) {
|
|
|
37
37
|
console.log(result.data);
|
|
38
38
|
}
|
|
39
39
|
```
|
|
40
|
+
|
|
41
|
+
### Error handling
|
|
42
|
+
|
|
43
|
+
Every response returns a discriminated union with either `data` or `error`. The `error` object follows the [RFC 7807](https://tools.ietf.org/html/rfc7807) Problem Details format, so you always get a machine-readable `code` and a human-readable `detail`.
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { createClient, searchSegments } from '@brigadasos/nadeshiko-sdk';
|
|
47
|
+
|
|
48
|
+
const client = createClient({
|
|
49
|
+
apiKey: process.env.NADESHIKO_API_KEY!,
|
|
50
|
+
baseUrl: 'PRODUCTION',
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const result = await searchSegments({
|
|
54
|
+
client,
|
|
55
|
+
body: { query: '食べる' },
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
if (result.error) {
|
|
59
|
+
switch (result.error.code) {
|
|
60
|
+
// 400 — Bad Request
|
|
61
|
+
case 'VALIDATION_FAILED':
|
|
62
|
+
console.error('Validation failed:', result.error.detail);
|
|
63
|
+
for (const [field, msg] of Object.entries(result.error.errors ?? {})) {
|
|
64
|
+
console.error(` ${field}: ${msg}`);
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
67
|
+
case 'INVALID_JSON':
|
|
68
|
+
console.error('Malformed JSON body:', result.error.detail);
|
|
69
|
+
break;
|
|
70
|
+
case 'INVALID_REQUEST':
|
|
71
|
+
console.error('Invalid request:', result.error.detail);
|
|
72
|
+
break;
|
|
73
|
+
|
|
74
|
+
// 401 — Unauthorized
|
|
75
|
+
case 'AUTH_CREDENTIALS_REQUIRED':
|
|
76
|
+
console.error('Missing API key or session token');
|
|
77
|
+
break;
|
|
78
|
+
case 'AUTH_CREDENTIALS_INVALID':
|
|
79
|
+
console.error('API key is invalid');
|
|
80
|
+
break;
|
|
81
|
+
case 'AUTH_CREDENTIALS_EXPIRED':
|
|
82
|
+
console.error('Token has expired, re-authenticate');
|
|
83
|
+
break;
|
|
84
|
+
case 'EMAIL_NOT_VERIFIED':
|
|
85
|
+
console.error('Email verification required');
|
|
86
|
+
break;
|
|
87
|
+
|
|
88
|
+
// 403 — Forbidden
|
|
89
|
+
case 'ACCESS_DENIED':
|
|
90
|
+
console.error('Access denied');
|
|
91
|
+
break;
|
|
92
|
+
case 'INSUFFICIENT_PERMISSIONS':
|
|
93
|
+
console.error('API key lacks the required scope');
|
|
94
|
+
break;
|
|
95
|
+
|
|
96
|
+
// 429 — Too Many Requests
|
|
97
|
+
case 'RATE_LIMIT_EXCEEDED':
|
|
98
|
+
console.error('Rate limit hit, slow down');
|
|
99
|
+
break;
|
|
100
|
+
case 'QUOTA_EXCEEDED':
|
|
101
|
+
console.error('Monthly quota exhausted');
|
|
102
|
+
break;
|
|
103
|
+
|
|
104
|
+
// 500 — Internal Server Error
|
|
105
|
+
case 'INTERNAL_SERVER_EXCEPTION':
|
|
106
|
+
console.error('Server error, trace ID:', result.error.instance);
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// result.data is fully typed as SearchResponse
|
|
113
|
+
for (const hit of result.data.results ?? []) {
|
|
114
|
+
console.log(hit.segment.ja.content, '—', hit.media.nameEn);
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### `throwOnError` mode
|
|
119
|
+
|
|
120
|
+
If you prefer exceptions over checking `.error`, pass `throwOnError: true`. The call will throw on any non-2xx response, and the return type narrows to just `{ data }`.
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
try {
|
|
124
|
+
const { data } = await searchSegments({
|
|
125
|
+
client,
|
|
126
|
+
throwOnError: true,
|
|
127
|
+
body: { query: '彼女' },
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
console.log(data.results);
|
|
131
|
+
} catch (error) {
|
|
132
|
+
console.error('Request failed:', error);
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
See [`examples/examples.ts`](examples/examples.ts) for more usage patterns.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@brigadasos/nadeshiko-sdk",
|
|
3
|
-
"version": "1.4.0-dev.
|
|
3
|
+
"version": "1.4.0-dev.7a7fd76",
|
|
4
4
|
"description": "TypeScript SDK for Nadeshiko API (internal build - includes internal endpoints)",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|