PageSourceSearch

https://slatex.ai/api/baas/v1/sdk.js

js slatex.ai collected 2026-10-05 13:51:07 UTC 3,998 bytes, 86 lines download raw bytes

1(function(){
2  var cfg = window.SLATEX_BAAS || {};
3  var base = (cfg.base || '/api/baas/v1').replace(/\/$/, '');
4  function handle(r){
5    return r.text().then(function(t){
6      var j; try { j = t ? JSON.parse(t) : null; } catch (e) { j = t; }
7      if (!r.ok) {
8        var err = new Error((j && j.message) || ('HTTP ' + r.status));
9        err.status = r.status; err.body = j; throw err;
10      }
11      return j;
12    });
13  }
14  function req(method, path, body){
15    var headers = { 'x-slatex-app-key': cfg.key };
16    if (body !== undefined) headers['Content-Type'] = 'application/json';
17    return fetch(base + '/' + cfg.appId + path, {
18      method: method,
19      headers: headers,
20      body: body !== undefined ? JSON.stringify(body) : undefined
21    }).then(handle);
22  }
23  // Read-shape unwrap: the server stores a record's fields under `data` and
24  // returns rows shaped { id, data:{...}, createdAt, updatedAt }, but
25  // create() takes a flat object. Flatten reads back to that same flat shape
26  // so list()/get() are symmetric with create() — callers read record.name,
27  // not record.data.name. Server id/updatedAt stay authoritative; a caller's
28  // own createdAt field (if any) is preserved.
29  function flatten(row){
30    if (!row || typeof row !== 'object' || !row.data || typeof row.data !== 'object') return row;
31    var out = {};
32    for (var k in row.data) { if (Object.prototype.hasOwnProperty.call(row.data, k)) out[k] = row.data[k]; }
33    out.id = row.id;
34    if (out.createdAt === undefined) out.createdAt = row.createdAt;
35    out.updatedAt = row.updatedAt;
36    return out;
37  }
38  function collection(name){
39    var c = '/collections/' + encodeURIComponent(name);
40    return {
41      create: function(data){ return req('POST', c, data); },
42      list: function(opts){
43        opts = opts || {};
44        var q = [];
45        if (opts.limit) q.push('limit=' + encodeURIComponent(opts.limit));
46        if (opts.offset) q.push('offset=' + encodeURIComponent(opts.offset));
47        return req('GET', c + (q.length ? ('?' + q.join('&')) : '')).then(function(rows){
48          return Array.isArray(rows) ? rows.map(flatten) : rows;
49        });
50      },
51      get: function(id){ return req('GET', c + '/' + encodeURIComponent(id)).then(flatten); },
52      update: function(id, data){ return req('PATCH', c + '/' + encodeURIComponent(id), data); },
53      remove: function(id){ return req('DELETE', c + '/' + encodeURIComponent(id)); }
54    };
55  }
56  function proxy(name, payload){
57    return req('POST', '/proxy/' + encodeURIComponent(name), payload || {});
58  }
59  var files = {
60    upload: function(file, opts){
61      opts = opts || {};
62      var coll = opts.collection ? ('/' + encodeURIComponent(opts.collection)) : '';
63      var fd = new FormData();
64      fd.append('file', file, (file && file.name) || opts.filename || 'file');
65      return fetch(base + '/' + cfg.appId + '/files' + coll, {
66        method: 'POST',
67        headers: { 'x-slatex-app-key': cfg.key },
68        body: fd
69      }).then(handle);
70    },
71    url: function(id){ return base + '/' + cfg.appId + '/files/' + encodeURIComponent(id); },
72    list: function(opts){
73      opts = opts || {};
74      var q = opts.collection ? ('?collection=' + encodeURIComponent(opts.collection)) : '';
75      return req('GET', '/files' + q);
76    },
77    remove: function(id){ return req('DELETE', '/files/' + encodeURIComponent(id)); },
78    // Make a file PUBLIC and get a link any reader can fetch WITHOUT the app
79    // key — required whenever you embed the file via a bare <img>/<a> or share
80    // the link with someone who never opened the app (e.g. a newspaper PDF).
81    // Resolves to { id, url, isPublic:true }; store/serve the returned url.
82    share: function(id){ return req('POST', '/files/' + encodeURIComponent(id) + '/share'); },
83    unshare: function(id){ return req('DELETE', '/files/' + encodeURIComponent(id) + '/share'); }
84  };
85  window.slatex = { collection: collection, proxy: proxy, files: files, config: cfg };
86})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.