@uidev1116/acms-js-sdk 0.0.2 → 0.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/README.md CHANGED
@@ -1,3 +1,196 @@
1
1
  # acms-js-sdk
2
2
 
3
- a-blog cms JavaScript SDK
3
+ JavaScript SDK for a-blog cms. Works in Node.js and modern browsers.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install acms-js-sdk
9
+
10
+ # or
11
+
12
+ yarn add acms-js-sdk
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ First, you need to create a client instance.
18
+
19
+ ES Modules:
20
+
21
+ ```js
22
+ import { createClient } from 'acms-js-sdk';
23
+
24
+ const acmsClient = createClient({
25
+ baseUrl: 'YOUR_BASE_URL',
26
+ apiKey: 'YOUR_API_KEY',
27
+ });
28
+ ```
29
+
30
+ CommonJS:
31
+
32
+ ```js
33
+ const { createClient } = require('acms-js-sdk');
34
+
35
+ const acmsClient = createClient({
36
+ baseUrl: 'YOUR_BASE_URL',
37
+ apiKey: 'YOUR_API_KEY',
38
+ });
39
+ ```
40
+
41
+ CDN:
42
+
43
+ ```html
44
+ <script type="module">
45
+ const { createClient } = 'https://unpkg.com/acms-js-sdk/dist/es/acms-js-sdk.js';
46
+
47
+ const acmsClient = createClient({
48
+ baseUrl: 'YOUR_BASE_URL',
49
+ apiKey: 'YOUR_API_KEY',
50
+ });
51
+ </script>
52
+ ```
53
+
54
+ Then, you can use `get` method.
55
+
56
+ Specify the module ID to be used in the module's GET API function in the `api`, and information on the specified module ID can be fetched.
57
+
58
+ ```js
59
+ acmsClient
60
+ .get({
61
+ api: 'MODULE_ID',
62
+ })
63
+ .then((response) => {
64
+ console.log(response.data);
65
+ })
66
+ .catch((error) => {
67
+ console.error(error);
68
+ });
69
+ ```
70
+
71
+ Relative paths can also be specified.
72
+
73
+ ```js
74
+ acmsClient
75
+ .get('api/MODULE_ID')
76
+ .then((response) => {
77
+ console.log(response.data);
78
+ })
79
+ .catch((error) => {
80
+ console.error(error);
81
+ });
82
+ ```
83
+
84
+ ### Url Context
85
+
86
+ You can specify the URL context.
87
+
88
+ ```js
89
+ acmsClient
90
+ .get({
91
+ blog: 'BLOG_CODE',
92
+ category: 'CATEGORY_CODE',
93
+ entry: 'ENTRY_CODE',
94
+ api: 'MODULE_ID',
95
+ })
96
+ .then((response) => {
97
+ console.log(response.data);
98
+ })
99
+ .catch((error) => {
100
+ console.error(error);
101
+ });
102
+ ```
103
+
104
+ ### Error Handling
105
+
106
+ You can handle errors.
107
+
108
+ Youb can check if the error is `AcmsFetchError` by using `isAcmsFetchError`.
109
+
110
+ ```js
111
+ acmsClient
112
+ .get({
113
+ api: 'MODULE_ID',
114
+ })
115
+ .then((response) => {
116
+ console.log(response.data);
117
+ })
118
+ .catch((error) => {
119
+ if (acmsClient.isAcmsFetchError(error)) {
120
+ console.error(error.response.data);
121
+ return;
122
+ }
123
+ console.error(error);
124
+ });
125
+ ```
126
+
127
+ ## acmsPath
128
+
129
+ You can get the path of Url Context by using utility function `acmsPath`.
130
+
131
+ ```js
132
+
133
+ import { acmsPath } from 'acms-js-sdk';
134
+
135
+ const path = acmsPath({
136
+ blog: 'BLOG_CODE',
137
+ category: 'CATEGORY_CODE',
138
+ entry: 'ENTRY_CODE',
139
+ // user: 1,
140
+ // tag: ['tag1', 'tag2'],
141
+ // field: 'color/eq/red',
142
+ // span: { start: '2021-01-01', end: '2021-12-31' },
143
+ // date: { year: 2021, month: 1, day: 1 },
144
+ // page: 1,
145
+ // order: 'id-asc',
146
+ // limit: 10,
147
+ // keyword: 'KEYWORD',
148
+ // tpl: 'include/sample.json'
149
+ api: 'MODULE_ID',
150
+ });
151
+ ```
152
+
153
+ ### Params Type
154
+
155
+ ```ts
156
+ interface AcmsContext {
157
+ blog?: string | number;
158
+ category?: string | string[] | number;
159
+ entry?: string | number;
160
+ user?: number;
161
+ tag?: string[];
162
+ field?: string;
163
+ span?: { start?: string | Date; end?: string | Date };
164
+ date?: { year?: number; month?: number; day?: number };
165
+ page?: number;
166
+ order?: string;
167
+ limit?: number;
168
+ keyword?: string;
169
+ tpl?: string;
170
+ api?: string;
171
+ searchParams?: ConstructorParameters<typeof URLSearchParams>[0];
172
+ }
173
+ ```
174
+
175
+ ## isAcmsFetchError
176
+
177
+ You can check if the error is `AcmsFetchError` by using utility function `isAcmsFetchError`.
178
+
179
+ ```js
180
+ import { isAcmsFetchError } from 'acms-js-sdk';
181
+
182
+ acmsClient
183
+ .get({
184
+ api: 'MODULE_ID',
185
+ })
186
+ .then((response) => {
187
+ console.log(response.data);
188
+ })
189
+ .catch((error) => {
190
+ if (isAcmsFetchError(error)) {
191
+ console.error(error.response.data);
192
+ return;
193
+ }
194
+ console.error(error);
195
+ });
196
+ ```
@@ -1,4 +1,4 @@
1
- "use strict";var P=Object.defineProperty;var q=(t,e,r)=>e in t?P(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var n=(t,e,r)=>(q(t,typeof e!="symbol"?e+"":e,r),r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("./acms-path.cjs"),o=require("../typeGuard-eqTQ9U-x.cjs"),c=require("../index-x-5Zn68j.cjs");async function b(t){try{const{message:e}=await t.json();return e??null}catch{return null}}async function A(){if(typeof fetch>"u"){const{default:t}=await Promise.resolve().then(()=>require("../browser-ponyfill-fsIF2jNb.cjs")).then(e=>e.browserPonyfill);return t}return fetch}async function F(...t){if(typeof Headers>"u"){const{Headers:e}=await Promise.resolve().then(()=>require("../browser-ponyfill-fsIF2jNb.cjs")).then(r=>r.browserPonyfill);return new e(...t)}return new Headers(...t)}const U={responseType:"json"};class ${constructor({baseUrl:e,apiKey:r,...i}){n(this,"baseUrl");n(this,"apiKey");n(this,"config");if(e==null&&e==="")throw new Error("baseUrl is required.");if(r==null&&r==="")throw new Error("apiKey is required.");if(!o.isString(e))throw new Error("baseUrl must be string.");if(!o.isString(r))throw new Error("apiKey must be string.");this.baseUrl=e,this.apiKey=r,this.config={...U,...i}}async request(e,r={}){const i={...this.config,...r},{requestInit:l,responseType:d}=i,y=await A(),m=this.createUrl(e),p=await this.createFetchOptions(l);try{const s=await y(m,p),{ok:E,status:a,statusText:u,headers:g}=s,h={data:await s[d](),status:a,statusText:u,headers:g};if(!E){const f=await b(s);return await Promise.reject(new c.AcmsFetchError(`fetch API response status: ${a}${f!=null?`
1
+ "use strict";var P=Object.defineProperty;var q=(t,e,r)=>e in t?P(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var n=(t,e,r)=>(q(t,typeof e!="symbol"?e+"":e,r),r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("./acms-path.cjs"),o=require("../typeGuard-eqTQ9U-x.cjs"),c=require("../index-x-5Zn68j.cjs");async function b(t){try{const{message:e}=await t.json();return e??null}catch{return null}}async function A(){if(typeof fetch>"u"){const{default:t}=await Promise.resolve().then(()=>require("../browser-ponyfill-fsIF2jNb.cjs")).then(e=>e.browserPonyfill);return t}return fetch}async function F(...t){if(typeof Headers>"u"){const{Headers:e}=await Promise.resolve().then(()=>require("../browser-ponyfill-fsIF2jNb.cjs")).then(r=>r.browserPonyfill);return new e(...t)}return new Headers(...t)}const U={responseType:"json"};class ${constructor({baseUrl:e,apiKey:r,...i}){n(this,"baseUrl");n(this,"apiKey");n(this,"config");if(e!=null&&e==="")throw new Error("baseUrl is required.");if(r!=null&&r==="")throw new Error("apiKey is required.");if(!o.isString(e))throw new Error("baseUrl must be string.");if(!o.isString(r))throw new Error("apiKey must be string.");this.baseUrl=e,this.apiKey=r,this.config={...U,...i}}async request(e,r={}){const i={...this.config,...r},{requestInit:l,responseType:d}=i,y=await A(),m=this.createUrl(e),p=await this.createFetchOptions(l);try{const s=await y(m,p),{ok:E,status:a,statusText:u,headers:g}=s,h={data:await s[d](),status:a,statusText:u,headers:g};if(!E){const f=await b(s);return await Promise.reject(new c.AcmsFetchError(`fetch API response status: ${a}${f!=null?`
2
2
  message is \`${f}\``:""}`,`${a} ${u}`,h))}return h}catch(s){return s instanceof Error?await Promise.reject(new Error(`Network Error.
3
3
  Details: ${s.message}`)):await Promise.reject(new Error(`Network Error.
4
4
  Details: Unknown Error`))}}async createFetchOptions(e){const r=await F(e==null?void 0:e.headers);return r.has("X-API-KEY")||r.set("X-API-KEY",this.apiKey),{...e,headers:r}}createUrl(e){return o.isString(e)?new URL(e,this.baseUrl):e instanceof URL?new URL(e,this.baseUrl):new URL(w({...e}),this.baseUrl)}async get(e,r={}){return await this.request(e,{...r,requestInit:{...r.requestInit,method:"GET"}})}static isAcmsFetchError(e){return c.isAcmsFetchError(e)}}function j(...t){return new $(...t)}exports.acmsPath=w;exports.isAcmsFetchError=c.isAcmsFetchError;exports.createClient=j;
@@ -38,9 +38,9 @@ class R {
38
38
  n(this, "baseUrl");
39
39
  n(this, "apiKey");
40
40
  n(this, "config");
41
- if (e == null && e === "")
41
+ if (e != null && e === "")
42
42
  throw new Error("baseUrl is required.");
43
- if (r == null && r === "")
43
+ if (r != null && r === "")
44
44
  throw new Error("apiKey is required.");
45
45
  if (!i(e))
46
46
  throw new Error("baseUrl must be string.");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uidev1116/acms-js-sdk",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "a-blog cms JavaScript SDK",
5
5
  "type": "module",
6
6
  "files": [