"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }/* Copyright 2024 Linkvite Labs */ var API_KEY_HEADER="X-API-Key",AUTH_TOKEN_HEADER= exports.AUTH_TOKEN_HEADER ="Authorization",DEFAULT_BASE_URL= exports.DEFAULT_BASE_URL ="https://api.linkvite.io";function lead(x){return x.charCodeAt(0)===47?x:"/"+x}function isObjectEmpty(object){for(let _ in object)return!1;return!0}function join(a,b){return a+lead(b)}function querystring(query){let usefulQuery=Object.entries(query).filter(entry=>{let[,value]=entry;return value!==void 0});if(usefulQuery.length===0)return"";let INITIAL_QUERYSTRING="?";return usefulQuery.reduce((acc,[key,value])=>{if(value===void 0)return acc;let result=`${key}=${value.toString()}`;return acc===INITIAL_QUERYSTRING?INITIAL_QUERYSTRING+result:acc+"&"+result},INITIAL_QUERYSTRING)}function createURLBuilder(base){let regex=/:[^/?#]+/g;return(path,query)=>{query={...query};let urlWithParams=path.replace(regex,param=>{if(param=param.substring(1),param in query){let{[param]:value,...rest}=query;if(query=rest,value===void 0)throw new Error(`URL param ${param} is undefined`);return typeof value=="number"?value.toString():value}throw new Error(`Missing param ${param}.`)}),urlWithSearch=isObjectEmpty(query)?urlWithParams:`${urlWithParams}${querystring(query)}`;return join(base,urlWithSearch)}}var _crossfetch = require('cross-fetch'); var ponyfill = _interopRequireWildcard(_crossfetch);var HAS_NATIVE_FETCH=typeof globalThis.fetch<"u",fetch= exports.fetch =HAS_NATIVE_FETCH?globalThis.fetch:ponyfill.default,Headers2= exports.Headers =HAS_NATIVE_FETCH?globalThis.Headers:ponyfill.Headers,Request2= exports.Request =HAS_NATIVE_FETCH?globalThis.Request:ponyfill.Request,Response2= exports.Response =HAS_NATIVE_FETCH?globalThis.Response:ponyfill.Response;function setAuthHeader(headers,options){return"key"in options?headers.set(API_KEY_HEADER,options.key):"token"in options&&headers.set(AUTH_TOKEN_HEADER,`Bearer ${options.token}`),headers}var LinkviteAPIError=class extends Error{constructor(request,response,data){super(data.message);this.request=request;this.response=response;this.data=data;this.status=response.status,this.ok=_nullishCoalesce(_nullishCoalesce(_optionalChain([data, 'optionalAccess', _2 => _2.ok]), () => (response.ok)), () => (!1))}},APIClient= exports.APIClient =class{constructor(options){this.options=options,this.url=createURLBuilder(_nullishCoalesce(options.baseUrl, () => (DEFAULT_BASE_URL))),this.agent=null}authType(){return"key"in this.options?"key":"token"}async get(path,query,init){return this.request("GET",path,void 0,query,init)}post(path,body,query,init){return this.request("POST",path,body,query,init)}patch(path,body,query,init){return this.request("PATCH",path,body,query,init)}delete(path,body,query,init){return this.request("DELETE",path,body,query,init)}async request(method,path,body,query={},init={}){let url=this.url(path,query),headers=setAuthHeader(new Headers2(init.headers),this.options);if(body){if(method==="GET")throw new Error("Cannot send a body with a GET request");body instanceof FormData?headers.delete("Content-Type"):headers.set("Content-Type","application/json")}let request=new Request2(url,{method,headers,body:body?body instanceof FormData?body:JSON.stringify(body):null,...init});return this.executeRequest(request)}async executeRequest(request){if(typeof process<"u"&&process.versions&&process.versions.node&&!this.agent){let https=await Promise.resolve().then(() => _interopRequireWildcard(require("https")));this.agent=new https.Agent({keepAlive:!0})}let response=await fetch(request,{keepalive:!0,headers:request.headers,agent:this.agent});if(response.status===204||!_optionalChain([response, 'access', _3 => _3.headers, 'access', _4 => _4.get, 'call', _5 => _5("Content-Type"), 'optionalAccess', _6 => _6.includes, 'call', _7 => _7("application/json")]))return;let result=await response.json().catch(error=>({ok:!1,message:error.message}));if(!result.ok)throw new LinkviteAPIError(request,response,result);return result.data}};function sdk(builder){return builder}var user=client=>({async get(){return await client.get("v1/user",{})},async getStorageUsage(){return await client.get("v1/user/storage",{})},async update(data){return await client.patch("v1/user",data,{})},async updateSettings(data){return await client.patch("v1/user/settings",data,{})},async emptyTrash(){return await client.delete("v1/user/trash",void 0,{})}});var apiKeys=client=>({async getAll(){return await client.get("v1/api-keys",{})},async create(data){return await client.post("v1/api-keys",data,{})},async update(id,data){return await client.patch("v1/api-keys/:id",data,{id})}});var bookmarks=client=>({async get(id){return await client.get("v1/bookmarks/:id",{id})},async getReader(id){return await client.get("v1/bookmarks/:id/reader",{id})},async getAll(p={}){return await client.get("v1/bookmarks",p)},async exists(url,collection=null){return await client.post("v1/bookmarks/exists",{url,collection},{})},async fromCollection(id){return await client.get("v1/bookmarks/from-collection/:id",{id})},async getArchive(id){return await client.get("v1/bookmarks/:id/archive",{id})},async createArchive(id){return await client.post("v1/bookmarks/:id/archive",void 0,{id})},async create(url,data={}){return await client.post("v1/bookmarks",{url,...data},{})},async createFromParsedData(data,collection=null){return await client.post("v1/bookmarks/from-parsed-link",{data,collection},{})},async createFromFile(data){let formData=new FormData;return formData.append("file",data.file),data.title&&formData.append("title",data.title),data.description&&formData.append("description",data.description),data.collection&&formData.append("collection",data.collection),data.tags&&formData.append("tags",data.tags),data.starred&&formData.append("starred",data.starred.toString()),data.allowComments&&formData.append("allow_comments",data.allowComments.toString()),await client.post("v1/bookmarks/file",formData,{})},async createFromTabs(data){return await client.post("v1/bookmarks/tabs",data,{})},async createFromEntry(data){let payload={};return data.tags?payload={...data,tags:data.tags.join(",")}:payload=data,await client.post("v1/bookmarks/manual",payload,{})},async reParse(id){return await client.post("v1/bookmarks/:id/re-parse",void 0,{id})},async toggleStar(id){return await client.post("v1/bookmarks/:id/star",void 0,{id})},async batchToggleStar(bookmarks2,value){return await client.post("v1/bookmarks/batch-star",{bookmarks:bookmarks2,value},{})},async batchUpdate(data){return await client.patch("v1/bookmarks/batch-edit",data,{})},async update(id,data){return await client.patch("v1/bookmarks/:id",data,{id})},async delete(id){return await client.delete("v1/bookmarks/:id",void 0,{id})}});var collections=client=>({async getOnlinePresence(id){return await client.get("v1/collections/:id/presence",{id})},async getAll(){return await client.get("v1/collections/all",{})},async get(id){return await client.get("v1/collections/:id",{id})},async getMembers(id){return await client.get("v1/collections/:id/members",{id})},async create(data){return await client.post("v1/collections",data,{})},async invite(id,{identifier,role}){return await client.post("v1/collections/:id/send-invite",{identifier,role},{id})},async leave(id){return await client.post("v1/collections/:id/leave",void 0,{id})},async move(id,data){return await client.post("v1/collections/:id/move",data,{id})},async update(id,data){return await client.patch("v1/collections/:id",data,{id})},async editRole(id,data){return await client.patch("v1/collections/:id/edit-role",data,{id})},async delete(id){return await client.delete("v1/collections/:id",void 0,{id})}});var Linkvite=class{constructor(optionsOrKey){if(!optionsOrKey||typeof optionsOrKey!="string"&&!("key"in optionsOrKey)&&!("token"in optionsOrKey))throw new Error("Missing authentication token to `new Linkvite()` \u2014 please provide a valid API Key, or Personal Access Token");let options;typeof optionsOrKey=="string"?options={key:optionsOrKey}:options=optionsOrKey,this.client=new APIClient(options),this.user=user(this.client),this.bookmarks=bookmarks(this.client),this.collections=collections(this.client),this.apiKeys=apiKeys(this.client)}};exports.APIClient = APIClient; exports.API_KEY_HEADER = API_KEY_HEADER; exports.AUTH_TOKEN_HEADER = AUTH_TOKEN_HEADER; exports.DEFAULT_BASE_URL = DEFAULT_BASE_URL; exports.HAS_NATIVE_FETCH = HAS_NATIVE_FETCH; exports.Headers = Headers2; exports.Linkvite = Linkvite; exports.LinkviteAPIError = LinkviteAPIError; exports.Request = Request2; exports.Response = Response2; exports.apiKeys = apiKeys; exports.bookmarks = bookmarks; exports.collections = collections; exports.createURLBuilder = createURLBuilder; exports.fetch = fetch; exports.isObjectEmpty = isObjectEmpty; exports.join = join; exports.lead = lead; exports.querystring = querystring; exports.sdk = sdk; exports.setAuthHeader = setAuthHeader; exports.user = user; //# sourceMappingURL=index.cjs.map