@taybart/corvid 0.0.6 → 0.0.8

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
@@ -11,18 +11,19 @@ type elOpts = {
11
11
  query?: string;
12
12
  type?: string;
13
13
  content?: any;
14
- parent?: HTMLElement;
14
+ parent?: HTMLElement | el;
15
15
  create?: boolean;
16
16
  };
17
17
  export declare class el {
18
18
  el: HTMLElement | null;
19
- name: string;
19
+ query: string;
20
20
  constructor(opts: string | elOpts);
21
21
  /*** get ***/
22
22
  value(): string;
23
23
  /*** set ***/
24
- parent(parent: HTMLElement): this;
25
- child(ch: HTMLElement): this;
24
+ parent(parent: HTMLElement | el): this;
25
+ appendChild(ch: HTMLElement | el): void;
26
+ child(ch: HTMLElement | el): this;
26
27
  inner(content: any, { force, text }?: {
27
28
  force?: boolean;
28
29
  text?: boolean;
@@ -34,12 +35,4 @@ export declare class el {
34
35
  listen(event: string, cb: (ev: Event) => void): this;
35
36
  onClick(cb: (ev: Event) => void): this;
36
37
  }
37
- /*** url params ***/
38
- export declare class params {
39
- params: URLSearchParams;
40
- constructor(p?: Object);
41
- set(p: Object): this;
42
- toString(): string;
43
- static render(p: Object): string;
44
- }
45
38
  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,21 @@ class el {
67
67
  return '';
68
68
  }
69
69
  parent(parent) {
70
- if (!this.el) throw new Error(`no element from input: ${this.name}`);
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
+ appendChild(ch) {
75
+ this.child(ch);
76
+ }
74
77
  child(ch) {
75
- var _this_el;
76
- if (!this.el) throw new Error(`no element from input: ${this.name}`);
77
- null == (_this_el = this.el) || _this_el.appendChild(ch);
78
+ if (!this.el) throw new Error(`no element from query: ${this.query}`);
79
+ if (ch instanceof el) this.el.appendChild(ch.el);
80
+ else this.el.appendChild(ch);
78
81
  return this;
79
82
  }
80
83
  inner(content, { force = false, text = false } = {}) {
81
- if (!this.el) throw new Error(`no element from input: ${this.name}`);
84
+ if (!this.el) throw new Error(`no element from query: ${this.query}`);
82
85
  if (this.el instanceof HTMLIFrameElement && !force) this.el.src = content;
83
86
  else if (this.el instanceof HTMLInputElement && !force) this.el.value = content;
84
87
  else if (text) this.el.textContent = content;
@@ -101,17 +104,17 @@ class el {
101
104
  return this;
102
105
  }
103
106
  addClass(className) {
104
- if (!this.el) throw new Error(`no element from input: ${this.name}`);
107
+ if (!this.el) throw new Error(`no element from query: ${this.query}`);
105
108
  this.el.classList.add(className);
106
109
  return this;
107
110
  }
108
111
  removeClass(className) {
109
- if (!this.el) throw new Error(`no element from input: ${this.name}`);
112
+ if (!this.el) throw new Error(`no element from query: ${this.query}`);
110
113
  this.el.classList.remove(className);
111
114
  return this;
112
115
  }
113
116
  listen(event, cb) {
114
- if (!this.el) throw new Error(`no element from input: ${this.name}`);
117
+ if (!this.el) throw new Error(`no element from query: ${this.query}`);
115
118
  this.el.addEventListener(event, cb);
116
119
  return this;
117
120
  }
@@ -120,45 +123,24 @@ class el {
120
123
  }
121
124
  constructor(opts){
122
125
  _define_property(this, "el", void 0);
123
- _define_property(this, "name", void 0);
126
+ _define_property(this, "query", void 0);
127
+ this.query = '';
124
128
  if ('string' == typeof opts) {
125
- this.name = opts;
129
+ this.query = opts;
126
130
  this.el = document.querySelector(opts);
127
131
  return;
128
132
  }
129
133
  const { query, type, content, parent, create } = opts;
130
134
  if (query) {
131
- this.name = query;
135
+ this.query = query;
132
136
  this.el = document.querySelector(query);
133
137
  if (!this.el && type && create) this.el = document.createElement(type);
134
- } else if (type) {
135
- this.name = type;
136
- this.el = document.createElement(type);
137
- } else throw new Error('no query or type provided');
138
+ } else if (type) this.el = document.createElement(type);
139
+ else throw new Error('no query or type provided');
138
140
  if (this.el && content) this.el.innerHTML = content;
139
141
  if (this.el && parent) parent.appendChild(this.el);
140
142
  }
141
143
  }
142
- class dom_params {
143
- set(p) {
144
- for (let [k, v] of Object.entries(p))this.params.set(k, v);
145
- return this;
146
- }
147
- toString() {
148
- return this.params.toString();
149
- }
150
- static render(p) {
151
- const params = new URLSearchParams();
152
- if (p) for (let [k, v] of Object.entries(p))params.set(k, v);
153
- return params.toString();
154
- }
155
- constructor(p){
156
- _define_property(this, "params", void 0);
157
- this.params = new URLSearchParams();
158
- if (p) for (let [k, v] of Object.entries(p))this.params.set(k, v);
159
- return this;
160
- }
161
- }
162
144
  function cssVar(name) {
163
145
  const style = window.getComputedStyle(document.body);
164
146
  return style.getPropertyValue(name);
@@ -191,6 +173,26 @@ function network_define_property(obj, key, value) {
191
173
  else obj[key] = value;
192
174
  return obj;
193
175
  }
176
+ class network_params {
177
+ set(p) {
178
+ for (let [k, v] of Object.entries(p))this.params.set(k, v);
179
+ return this;
180
+ }
181
+ toString() {
182
+ return this.params.toString();
183
+ }
184
+ static render(p) {
185
+ const params = new URLSearchParams();
186
+ if (p) for (let [k, v] of Object.entries(p))params.set(k, v);
187
+ return params.toString();
188
+ }
189
+ constructor(p){
190
+ network_define_property(this, "params", void 0);
191
+ this.params = new URLSearchParams();
192
+ if (p) for (let [k, v] of Object.entries(p))this.params.set(k, v);
193
+ return this;
194
+ }
195
+ }
194
196
  class request {
195
197
  auth(token) {
196
198
  this.opts.headers.Authorization = `Bearer ${token}`;
@@ -204,11 +206,14 @@ class request {
204
206
  this.opts.body = body;
205
207
  return this;
206
208
  }
207
- async do({ path, overrideBody, overrideSuccess } = {}) {
209
+ async do({ path, override } = {}) {
208
210
  if (this.opts.auth) this.opts.headers.Authorization = `Bearer ${this.opts.auth}`;
209
- const body = overrideBody || this.opts.body;
211
+ if (!override) override = {};
212
+ const body = override.body || this.opts.body;
210
213
  let url = this.opts.url;
211
214
  if (path) url = `${this.opts.url}${path}`;
215
+ const params = override.params || this.opts.params;
216
+ if (params) url = `${url}?${params.toString()}`;
212
217
  const res = await fetch(url, {
213
218
  method: this.opts.method,
214
219
  headers: {
@@ -218,14 +223,14 @@ class request {
218
223
  },
219
224
  body: JSON.stringify(body)
220
225
  });
221
- const success = overrideSuccess || this.opts.success;
226
+ const success = override.success || this.opts.success;
222
227
  if (res.status !== success) {
223
228
  const body = await res.json();
224
- throw new Error(`bad response ${res.status} !== ${this.opts.success}, body: ${body}`);
229
+ throw new Error(`bad response ${res.status} !== ${success}, body: ${body}`);
225
230
  }
226
231
  return await res.json();
227
232
  }
228
- constructor(opts){
233
+ constructor(opts = {}){
229
234
  network_define_property(this, "opts", void 0);
230
235
  if (opts.type && 'json' !== opts.type) throw new Error('this class only provides json requests');
231
236
  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.6",
3
+ "version": "0.0.8",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "exports": {