@taybart/corvid 0.0.5 → 0.0.7

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/dist/dom.d.ts CHANGED
@@ -16,6 +16,7 @@ type elOpts = {
16
16
  };
17
17
  export declare class el {
18
18
  el: HTMLElement | null;
19
+ query: string;
19
20
  constructor(opts: string | elOpts);
20
21
  /*** get ***/
21
22
  value(): string;
@@ -28,15 +29,9 @@ export declare class el {
28
29
  }): this;
29
30
  src(url: string): this;
30
31
  style(style: Object | string): this;
32
+ addClass(className: string): this;
33
+ removeClass(className: string): this;
31
34
  listen(event: string, cb: (ev: Event) => void): this;
32
35
  onClick(cb: (ev: Event) => void): this;
33
36
  }
34
- /*** url params ***/
35
- export declare class params {
36
- params: URLSearchParams;
37
- constructor(p?: Object);
38
- set(p: Object): this;
39
- toString(): string;
40
- static render(p: Object): string;
41
- }
42
37
  export {};
package/dist/index.js CHANGED
@@ -24,7 +24,6 @@ var dom_namespaceObject = {};
24
24
  __webpack_require__.r(dom_namespaceObject);
25
25
  __webpack_require__.d(dom_namespaceObject, {
26
26
  el: ()=>el,
27
- params: ()=>dom_params,
28
27
  ready: ()=>ready
29
28
  });
30
29
  var style_namespaceObject = {};
@@ -39,6 +38,7 @@ __webpack_require__.d(style_namespaceObject, {
39
38
  var network_namespaceObject = {};
40
39
  __webpack_require__.r(network_namespaceObject);
41
40
  __webpack_require__.d(network_namespaceObject, {
41
+ params: ()=>network_params,
42
42
  request: ()=>request
43
43
  });
44
44
  var local_storage_namespaceObject = {};
@@ -67,18 +67,18 @@ class el {
67
67
  return '';
68
68
  }
69
69
  parent(parent) {
70
- if (!this.el) throw new Error('no element');
70
+ if (!this.el) throw new Error(`no element from query: ${this.query}`);
71
71
  parent.appendChild(this.el);
72
72
  return this;
73
73
  }
74
74
  child(ch) {
75
75
  var _this_el;
76
- if (!this.el) throw new Error('no element');
76
+ if (!this.el) throw new Error(`no element from query: ${this.query}`);
77
77
  null == (_this_el = this.el) || _this_el.appendChild(ch);
78
78
  return this;
79
79
  }
80
80
  inner(content, { force = false, text = false } = {}) {
81
- if (!this.el) throw new Error('no element');
81
+ if (!this.el) throw new Error(`no element from query: ${this.query}`);
82
82
  if (this.el instanceof HTMLIFrameElement && !force) this.el.src = content;
83
83
  else if (this.el instanceof HTMLInputElement && !force) this.el.value = content;
84
84
  else if (text) this.el.textContent = content;
@@ -100,8 +100,18 @@ class el {
100
100
  }
101
101
  return this;
102
102
  }
103
+ addClass(className) {
104
+ if (!this.el) throw new Error(`no element from query: ${this.query}`);
105
+ this.el.classList.add(className);
106
+ return this;
107
+ }
108
+ removeClass(className) {
109
+ if (!this.el) throw new Error(`no element from query: ${this.query}`);
110
+ this.el.classList.remove(className);
111
+ return this;
112
+ }
103
113
  listen(event, cb) {
104
- if (!this.el) throw new Error('no element');
114
+ if (!this.el) throw new Error(`no element from query: ${this.query}`);
105
115
  this.el.addEventListener(event, cb);
106
116
  return this;
107
117
  }
@@ -110,12 +120,16 @@ class el {
110
120
  }
111
121
  constructor(opts){
112
122
  _define_property(this, "el", void 0);
123
+ _define_property(this, "query", void 0);
124
+ this.query = '';
113
125
  if ('string' == typeof opts) {
126
+ this.query = opts;
114
127
  this.el = document.querySelector(opts);
115
128
  return;
116
129
  }
117
130
  const { query, type, content, parent, create } = opts;
118
131
  if (query) {
132
+ this.query = query;
119
133
  this.el = document.querySelector(query);
120
134
  if (!this.el && type && create) this.el = document.createElement(type);
121
135
  } else if (type) this.el = document.createElement(type);
@@ -124,26 +138,6 @@ class el {
124
138
  if (this.el && parent) parent.appendChild(this.el);
125
139
  }
126
140
  }
127
- class dom_params {
128
- set(p) {
129
- for (let [k, v] of Object.entries(p))this.params.set(k, v);
130
- return this;
131
- }
132
- toString() {
133
- return this.params.toString();
134
- }
135
- static render(p) {
136
- const params = new URLSearchParams();
137
- if (p) for (let [k, v] of Object.entries(p))params.set(k, v);
138
- return params.toString();
139
- }
140
- constructor(p){
141
- _define_property(this, "params", void 0);
142
- this.params = new URLSearchParams();
143
- if (p) for (let [k, v] of Object.entries(p))this.params.set(k, v);
144
- return this;
145
- }
146
- }
147
141
  function cssVar(name) {
148
142
  const style = window.getComputedStyle(document.body);
149
143
  return style.getPropertyValue(name);
@@ -176,6 +170,26 @@ function network_define_property(obj, key, value) {
176
170
  else obj[key] = value;
177
171
  return obj;
178
172
  }
173
+ class network_params {
174
+ set(p) {
175
+ for (let [k, v] of Object.entries(p))this.params.set(k, v);
176
+ return this;
177
+ }
178
+ toString() {
179
+ return this.params.toString();
180
+ }
181
+ static render(p) {
182
+ const params = new URLSearchParams();
183
+ if (p) for (let [k, v] of Object.entries(p))params.set(k, v);
184
+ return params.toString();
185
+ }
186
+ constructor(p){
187
+ network_define_property(this, "params", void 0);
188
+ this.params = new URLSearchParams();
189
+ if (p) for (let [k, v] of Object.entries(p))this.params.set(k, v);
190
+ return this;
191
+ }
192
+ }
179
193
  class request {
180
194
  auth(token) {
181
195
  this.opts.headers.Authorization = `Bearer ${token}`;
@@ -189,11 +203,14 @@ class request {
189
203
  this.opts.body = body;
190
204
  return this;
191
205
  }
192
- async do({ path, overrideBody, overrideSuccess } = {}) {
206
+ async do({ path, override } = {}) {
193
207
  if (this.opts.auth) this.opts.headers.Authorization = `Bearer ${this.opts.auth}`;
194
- const body = overrideBody || this.opts.body;
208
+ if (!override) override = {};
209
+ const body = override.body || this.opts.body;
195
210
  let url = this.opts.url;
196
211
  if (path) url = `${this.opts.url}${path}`;
212
+ const params = override.params || this.opts.params;
213
+ if (params) url = `${url}?${params.toString()}`;
197
214
  const res = await fetch(url, {
198
215
  method: this.opts.method,
199
216
  headers: {
@@ -203,14 +220,14 @@ class request {
203
220
  },
204
221
  body: JSON.stringify(body)
205
222
  });
206
- const success = overrideSuccess || this.opts.success;
223
+ const success = override.success || this.opts.success;
207
224
  if (res.status !== success) {
208
225
  const body = await res.json();
209
- throw new Error(`bad response ${res.status} !== ${this.opts.success}, body: ${body}`);
226
+ throw new Error(`bad response ${res.status} !== ${success}, body: ${body}`);
210
227
  }
211
228
  return await res.json();
212
229
  }
213
- constructor(opts){
230
+ constructor(opts = {}){
214
231
  network_define_property(this, "opts", void 0);
215
232
  if (opts.type && 'json' !== opts.type) throw new Error('this class only provides json requests');
216
233
  if (!opts.url) throw new Error('must provide url');
package/dist/network.d.ts CHANGED
@@ -1,21 +1,33 @@
1
+ /*** url params ***/
2
+ export declare class params {
3
+ params: URLSearchParams;
4
+ constructor(p?: Object);
5
+ set(p: Object): this;
6
+ toString(): string;
7
+ static render(p: Object): string;
8
+ }
1
9
  export type requestOpts = {
2
- url: string;
3
- type: 'json';
4
- method: 'GET' | 'POST' | 'PUT' | 'DELETE';
5
- headers: Record<string, string>;
6
- auth: string;
7
- body: Object;
8
- success: number;
10
+ url?: string;
11
+ type?: 'json';
12
+ method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
13
+ params?: typeof params;
14
+ headers?: Record<string, string>;
15
+ auth?: string;
16
+ body?: Object;
17
+ success?: number;
9
18
  };
10
19
  export declare class request {
11
20
  opts: requestOpts;
12
- constructor(opts: requestOpts);
21
+ constructor(opts?: requestOpts);
13
22
  auth(token: string): this;
14
23
  basicAuth(username: string, password: string): this;
15
24
  body(body: Object): this;
16
- do({ path, overrideBody, overrideSuccess, }?: {
25
+ do({ path, override, }?: {
17
26
  path?: string;
18
- overrideBody?: Object;
19
- overrideSuccess?: number;
27
+ override?: {
28
+ success?: number;
29
+ params?: any;
30
+ body?: Object;
31
+ };
20
32
  }): Promise<any>;
21
33
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taybart/corvid",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "exports": {