Changed Syntax, updated Utils implementation
This commit is contained in:
parent
df41bf428b
commit
6033e8fc14
@ -1,4 +1,3 @@
|
||||
|
||||
local tokens = {
|
||||
["exampleToken"] = ""
|
||||
}
|
||||
@ -7,122 +6,60 @@ local tokens = {
|
||||
-- DO NOT EDIT THE FOLLOWING LINES BELOW, UNLESS YOU KNOW WHAT YOU ARE DOING!
|
||||
|
||||
local Utils = require(script.Parent.Utils)
|
||||
|
||||
type Function = () -> ()
|
||||
type DefaultResultType = (success: boolean, errorMessage: string) -> ()
|
||||
type DefaultResult = {
|
||||
["onResult"]: {
|
||||
["Connect"]:DefaultResultType
|
||||
}
|
||||
}
|
||||
|
||||
local HttpService = game:GetService("HttpService")
|
||||
|
||||
function signal()
|
||||
local RBXSignal = {}
|
||||
RBXSignal._bindableEvent = Instance.new("BindableEvent")
|
||||
function RBXSignal:_Fire(...)
|
||||
RBXSignal._bindableEvent:Fire(...)
|
||||
end
|
||||
function RBXSignal:Connect(handler: ({success: boolean, result: {}}) -> ({success: boolean, result: {}}))
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
if not (type(handler) == "function") then
|
||||
error(("connect(%s)"):format(typeof(handler)), 2)
|
||||
end
|
||||
RBXSignal._bindableEvent.Event:Connect(function(...)
|
||||
handler(...)
|
||||
end)
|
||||
end
|
||||
return RBXSignal
|
||||
end
|
||||
|
||||
local MarcSyncClient = {}
|
||||
|
||||
MarcSyncClient.getVersion = function(self:typeof(MarcSyncClient), clientId: number?):{["success"]:boolean,["version"]:string,["update_server"]:string}
|
||||
self:_checkInstallation()
|
||||
local url = ""
|
||||
if clientId then url = "/"..clientId end
|
||||
local result = nil;
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("GET", "https://api.marcsync.dev/v0/utils/version"..url)).onResult:Connect(function(any)
|
||||
result = any
|
||||
end)
|
||||
repeat
|
||||
task.wait()
|
||||
until result ~= nil
|
||||
local result = Utils.makeHTTPRequest("GET", "https://api.marcsync.dev/v0/utils/version"..url);
|
||||
return result
|
||||
end
|
||||
|
||||
MarcSyncClient.createCollection = function(self:typeof(MarcSyncClient), collectionName: string):typeof(require(script.Parent.Objects.Collection))
|
||||
MarcSyncClient.createCollection = function(self:typeof(MarcSyncClient), collectionName: string):typeof(require(script.Parent.Objects.Collection).new())
|
||||
if not self._accessToken then error("[MarcSync] Please set a Token before using MarcSync.") end
|
||||
if not collectionName then error("No CollectionName Provided") end
|
||||
local result = nil;
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("POST", "https://api.marcsync.dev/v0/collection/"..collectionName, {}, self._accessToken)).onResult:Connect(function(any)
|
||||
if not any["success"] then result = false return end
|
||||
result = require(script.Parent.Objects.Collection)._new(collectionName, self._accessToken)
|
||||
end)
|
||||
repeat
|
||||
task.wait()
|
||||
until result ~= nil
|
||||
local result = Utils.makeHTTPRequest("collection", "POST", "https://api.marcsync.dev/v0/collection/"..collectionName, {}, self._accessToken);
|
||||
|
||||
if not result["success"] then error(result["errorMessage"]) end
|
||||
result = require(script.Parent.Objects.Collection).new(collectionName, self._accessToken)
|
||||
|
||||
return result
|
||||
end
|
||||
MarcSyncClient.fetchCollection = function(self:typeof(MarcSyncClient), collectionName: string):typeof(require(script.Parent.Objects.Collection))
|
||||
MarcSyncClient.fetchCollection = function(self:typeof(MarcSyncClient), collectionName: string):typeof(require(script.Parent.Objects.Collection).new())
|
||||
self:_checkInstallation()
|
||||
if not collectionName then error("No CollectionName Provided") end
|
||||
local result = nil;
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("GET", "https://api.marcsync.dev/v0/collection/"..collectionName, {}, self._accessToken)).onResult:Connect(function(any)
|
||||
if not any["success"] then result = false return end
|
||||
result = require(script.Parent.Objects.Collection)._new(collectionName, self._accessToken)
|
||||
end)
|
||||
repeat
|
||||
task.wait()
|
||||
until result ~= nil
|
||||
local result = Utils.makeHTTPRequest("collection", "GET", "https://api.marcsync.dev/v0/collection/"..collectionName, {}, self._accessToken);
|
||||
|
||||
if not result["success"] then error(result["errorMessage"]) end
|
||||
result = require(script.Parent.Objects.Collection).new(collectionName, self._accessToken)
|
||||
|
||||
return result
|
||||
end
|
||||
MarcSyncClient.getCollection = function(self:typeof(MarcSyncClient), collectionName: string):typeof(require(script.Parent.Objects.Collection))
|
||||
MarcSyncClient.getCollection = function(self:typeof(MarcSyncClient), collectionName: string):typeof(require(script.Parent.Objects.Collection).new())
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
self:_checkInstallation()
|
||||
if not collectionName then error("No CollectionName Provided") end
|
||||
return require(script.Parent.Objects.Collection)._new(collectionName, self._accessToken)
|
||||
return require(script.Parent.Objects.Collection).new(collectionName, self._accessToken)
|
||||
end
|
||||
|
||||
return function(accessToken: string):typeof(MarcSyncClient)
|
||||
if not accessToken then warn("Token not provided while creating a new MarcSync Object.") end
|
||||
if not tokens[accessToken] then warn("Token provided for creating a new MarcSync Object not Found in Token Table, using it as token instead.") else accessToken = tokens[accessToken] end
|
||||
local self = setmetatable({}, MarcSyncClient)
|
||||
self._accessToken = accessToken
|
||||
self._checkInstallation = function()
|
||||
if not self then error("Please Setup MarcSync before using MarcSync.") end
|
||||
if not self._accessToken then error("[MarcSync] Please set a Token before using MarcSync.") end
|
||||
--print(HttpService.HttpEnabled)
|
||||
--if not HttpService.HttpEnabled then error("Please Enable HTTPService in order to use MarcSync.") end
|
||||
end
|
||||
self._errorHandler = function(RBXSignal: {}, Error: string)
|
||||
task.spawn(function()
|
||||
RBXSignal:_Fire({
|
||||
["success"] = false,
|
||||
["errorMessage"] = Error
|
||||
})
|
||||
end)
|
||||
return {["onResult"] = RBXSignal}
|
||||
end
|
||||
self._requestHandler = function(result: {}, Error: string):{["onResult"]: {}}
|
||||
if result == nil then return self._errorHandler(signal(), Error) end
|
||||
local errorResult;
|
||||
local signalevent;
|
||||
if #result.Body == 0 and not result.Success then
|
||||
errorResult = "HTTP "..result.StatusCode.." ("..result.StatusMessage..")" result = false
|
||||
elseif not pcall(function() HttpService:JSONDecode(result.Body) end) then
|
||||
error("Unexpected MarcSync Result.")
|
||||
else
|
||||
result = result.Body
|
||||
signalevent = {["onResult"]=signal()}
|
||||
return {
|
||||
new = function(accessToken: string):typeof(MarcSyncClient)
|
||||
if not accessToken then warn("Token not provided while creating a new MarcSync Object.") end
|
||||
if not tokens[accessToken] then warn("Token provided for creating a new MarcSync Object not Found in Token Table, using it as token instead.") else accessToken = tokens[accessToken] end
|
||||
local self = {}
|
||||
self._accessToken = accessToken
|
||||
self._checkInstallation = function()
|
||||
if not self then error("Please Setup MarcSync before using MarcSync.") end
|
||||
if not self._accessToken then error("[MarcSync] Please set a Token before using MarcSync.") end
|
||||
--print(HttpService.HttpEnabled)
|
||||
--if not HttpService.HttpEnabled then error("Please Enable HTTPService in order to use MarcSync.") end
|
||||
end
|
||||
task.spawn(function()
|
||||
if not result then return end
|
||||
signalevent.onResult:_Fire(result)
|
||||
end)
|
||||
return signalevent or self._errorHandler(signal(), errorResult)
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
self = setmetatable(self, {
|
||||
__index = MarcSyncClient
|
||||
})
|
||||
|
||||
return self
|
||||
end
|
||||
}
|
@ -1,109 +1,78 @@
|
||||
local HttpService = game:GetService("HttpService")
|
||||
local Utils = require(script.Parent.Parent.Utils)
|
||||
local Entry = require(script.Parent.Entry)
|
||||
|
||||
local collection = {}
|
||||
collection.__index = collection
|
||||
|
||||
local filterSheme = {
|
||||
["values"]=typeof({ ["key"]=... }),
|
||||
["startsWith"]=typeof({ "key" }),
|
||||
["ignoreCases"]=typeof({ "key" })
|
||||
local types = {
|
||||
EntryData = require(script.Parent.Parent.Types.EntryData).getType()
|
||||
}
|
||||
|
||||
function collection:insert(data:{key:any}):typeof(Entry)?
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
local Collection = {}
|
||||
|
||||
Collection.createEntry = function(self:typeof(Collection), data:typeof(types.EntryData)):typeof(Entry.new())
|
||||
if not self._collectionName then error("[MarcSync: Collection] Invalid Object created or trying to access an destroied object.") end
|
||||
local result = nil;
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("POST", "https://api.marcsync.dev/v0/entries/"..self._collectionName, {["data"]=data}, self._token)).onResult:Connect(function(any)
|
||||
if any["success"] and any["objectId"] then
|
||||
data["_id"] = any["objectId"]
|
||||
result = require(script.Parent.Entry)._new(self._collectionName, data, self._token)
|
||||
return
|
||||
local result = Utils.makeHTTPRequest("entry", "POST", "https://api.marcsync.dev/v0/entries/"..self._collectionName, {["data"]=data}, self._accessToken);
|
||||
|
||||
if result["success"] and result["objectId"] then
|
||||
data["_id"] = result["objectId"]
|
||||
result = require(script.Parent.Entry).new(self._collectionName, data, self._accessToken)
|
||||
else
|
||||
error(result["errorMessage"])
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
Collection.updateEntries = function(self:typeof(Collection), filters:typeof(types.EntryData), data:typeof(types.EntryData)):number
|
||||
if not self._collectionName then error("[MarcSync: Collection] Invalid Object created or trying to access an destroied object.") end
|
||||
local result = Utils.makeHTTPRequest("entry", "PUT", "https://api.marcsync.dev/v0/entries/"..self._collectionName, {["filters"]=filters,["data"]=data}, self._accessToken);
|
||||
if not result["success"] then error(result["errorMessage"]) end
|
||||
|
||||
return result["modifiedEntries"]
|
||||
end
|
||||
|
||||
Collection.getEntries = function(self:typeof(Collection), filters:typeof(types.EntryData)):{[number]:typeof(Entry.new())}
|
||||
if not self._collectionName then error("[MarcSync: Collection] Invalid Object created or trying to access an destroied object.") end
|
||||
if not filters then filters = {} end
|
||||
local result = Utils.makeHTTPRequest("entry", "DELETE", "https://api.marcsync.dev/v0/entries/"..self._collectionName.."?isQuery=true", {["filters"]=filters}, self._accessToken);
|
||||
if result["success"] and result["entries"] then
|
||||
local _result = {}
|
||||
for index,entry in pairs(result["entries"]) do
|
||||
_result[index] = require(script.Parent.Entry).new(self._collectionName, entry, self._accessToken)
|
||||
end
|
||||
result = any
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
result = _result
|
||||
else
|
||||
error(result["errorMessage"])
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
function collection:update(filters:typeof(filterSheme),data:{key:any}):{message:string?,errorMessage:string?,success:boolean,modifiedEntries:number?}
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
Collection.deleteEntries = function(self:typeof(Collection), filters:typeof(types.EntryData)):number
|
||||
if not self._collectionName then error("[MarcSync: Collection] Invalid Object created or trying to access an destroied object.") end
|
||||
local result = nil;
|
||||
if not filters.values then error("[MarcSync: Collection] Invalid arguments given for collection:select(). Expected filters.values, got nil") end
|
||||
filters.values["_startsWith"] = filters.startsWith or {}
|
||||
filters.values["_ignoreCases"] = filters.ignoreCases or {}
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("PUT", "https://api.marcsync.dev/v1/entries/"..self._collectionName, {["filters"]=filters.values,["data"]=data}, self._token)).onResult:Connect(function(any)
|
||||
result = any
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
return result
|
||||
local result = Utils.makeHTTPRequest("DELETE", "https://api.marcsync.dev/v0/entries/"..self._collectionName, {["filters"]=filters}, self._accessToken);
|
||||
if not result["success"] then error(result["errorMessage"]) end
|
||||
|
||||
return result["deletedEntries"]
|
||||
end
|
||||
|
||||
function collection:select(filters:typeof(filterSheme),limit:number):{entries:{a:typeof(Entry)?,b:typeof(Entry)?,c:typeof(Entry)?}?,success:boolean,message:string?,errorMessage:string?}
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
Collection.drop = function(self:typeof(Collection))
|
||||
if not self._collectionName then error("[MarcSync: Collection] Invalid Object created or trying to access an destroied object.") end
|
||||
local result = nil;
|
||||
if not filters.values then error("[MarcSync: Collection] Invalid arguments given for collection:select(). Expected filters.values, got nil") end
|
||||
filters.values["_startsWith"] = filters.startsWith or {}
|
||||
filters.values["_ignoreCases"] = filters.ignoreCases or {}
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("PATCH", "https://api.marcsync.dev/v1/entries/"..self._collectionName.."?methodOverwrite=GET", {["filters"]=filters.values, ["limit"]=limit}, self._token)).onResult:Connect(function(any)
|
||||
if any["success"] and any["entries"] then
|
||||
local _result = {["entries"]={},["success"]=true}
|
||||
for index,entry in pairs(any["entries"]) do
|
||||
_result["entries"][index] = require(script.Parent.Entry)._new(self._collectionName, entry, self._token)
|
||||
end
|
||||
result = _result
|
||||
return
|
||||
end
|
||||
result = any
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
return result
|
||||
local result = Utils.makeHTTPRequest("collection", "DELETE", "https://api.marcsync.dev/v0/collection/"..self._collectionName, {}, self._accessToken);
|
||||
if not result["success"] then error(result["errorMessage"]) end
|
||||
self._collectionName = nil
|
||||
self._accessToken = nil
|
||||
end
|
||||
|
||||
function collection:delete(filters:typeof(filterSheme)):{message:string?,errorMessage:string?,success:boolean,deletedEntries:number?}
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
if not self._collectionName then error("[MarcSync: Collection] Invalid Object created or trying to access an destroied object.") end
|
||||
local result = nil;
|
||||
if not filters.values then error("[MarcSync: Collection] Invalid arguments given for collection:select(). Expected filters.values, got nil") end
|
||||
filters.values["_startsWith"] = filters.startsWith or {}
|
||||
filters.values["_ignoreCases"] = filters.ignoreCases or {}
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("DELETE", "https://api.marcsync.dev/v1/entries/"..self._collectionName, {["filters"]=filters.values}, self._token)).onResult:Connect(function(any)
|
||||
result = any
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
return result
|
||||
end
|
||||
return {
|
||||
new = function(collectionName: string, accessToken: string):typeof(Collection)
|
||||
local self = {}
|
||||
self._collectionName = collectionName
|
||||
self._accessToken = accessToken
|
||||
|
||||
function collection:drop():{success:boolean,message:string?,errorMessage:string?}
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
if not self._collectionName then error("[MarcSync: Collection] Invalid Object created or trying to access an destroied object.") end
|
||||
local result = nil;
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("DELETE", "https://api.marcsync.dev/v0/collection/"..self._collectionName, {}, self._token)).onResult:Connect(function(any)
|
||||
result = any
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
return result
|
||||
end
|
||||
self = setmetatable(self, {
|
||||
__index = Collection
|
||||
})
|
||||
|
||||
collection._collectionName = nil
|
||||
|
||||
function collection._new(_collectionName: string, _token: string):typeof(collection)
|
||||
local self = setmetatable({}, collection)
|
||||
self._collectionName = _collectionName
|
||||
self._token = _token
|
||||
return self
|
||||
end
|
||||
|
||||
return collection
|
||||
return self
|
||||
end
|
||||
}
|
@ -1,62 +1,56 @@
|
||||
local Utils = require(script.Parent.Parent.Utils)
|
||||
|
||||
local entry = {}
|
||||
entry.__index = entry
|
||||
local types = {
|
||||
EntryData = require(script.Parent.Parent.Types.EntryData).getType()
|
||||
}
|
||||
|
||||
function entry:getValue(value:string):any
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
local Entry = {}
|
||||
|
||||
Entry.getValue = function(self:typeof(Entry), value:string):any
|
||||
if not value then return nil end
|
||||
return self._values[value]
|
||||
return self._entryData[value]
|
||||
end
|
||||
|
||||
function entry:getValues():{}
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
return self._values
|
||||
Entry.getValues = function(self:typeof(Entry)):typeof(types.EntryData)
|
||||
return self._entryData
|
||||
end
|
||||
|
||||
function entry:update(newData:{key:any}):{success:boolean,errorMessage:string?,message:string?,modifiedEntries:IntValue?}
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
local result = nil;
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("PUT", "https://api.marcsync.dev/v0/entries/"..self._tableId, {["filters"]={["_id"]=self._objectId},["data"]=newData}, self._token)).onResult:Connect(function(any)
|
||||
if any["success"] and any["modifiedEntries"] and any["modifiedEntries"] > 0 then
|
||||
for i,v in pairs(newData) do
|
||||
self._values[i] = v
|
||||
end
|
||||
Entry.updateValues = function(self:typeof(Entry), values:typeof(types.EntryData)):number
|
||||
local result = Utils.makeHTTPRequest("entryId", "PUT", "https://api.marcsync.dev/v0/entries/"..self._tableId, {["filters"]={["_id"]=self._objectId},["data"]=values}, self._accessToken);
|
||||
|
||||
if result["success"] and result["modifiedEntries"] and result["modifiedEntries"] > 0 then
|
||||
for i,v in pairs(values) do
|
||||
self._entryData[i] = v
|
||||
end
|
||||
result = any
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
return result
|
||||
elseif not result["success"] then
|
||||
error(result["errorMessage"])
|
||||
end
|
||||
|
||||
return result["modifiedEntries"]
|
||||
end
|
||||
|
||||
function entry:delete():{success:boolean,errorMessage:string?,message:string?,deletedEntries:IntValue?}
|
||||
Entry.delete = function(self:typeof(Entry))
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
local result = nil;
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("DELETE", "https://api.marcsync.dev/v0/entries/"..self._tableId, {["filters"]={["_id"]=self._objectId}}, self._token)).onResult:Connect(function(any)
|
||||
if any["success"] and any["deletedEntries"] then
|
||||
if any["deletedEntries"] < 1 then result = false return end
|
||||
spawn(function()
|
||||
self = nil
|
||||
end)
|
||||
end
|
||||
result = any
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
return result
|
||||
local result = Utils.makeHTTPRequest("entryId", "DELETE", "https://api.marcsync.dev/v0/entries/"..self._tableId, {["filters"]={["_id"]=self._objectId}}, self._accessToken);
|
||||
|
||||
if not result["success"] then error(result["errorMessage"]) end
|
||||
self = nil
|
||||
|
||||
end
|
||||
|
||||
function entry._new(_tableId:string, _values:{}, _token:string):typeof(entry)
|
||||
if not _tableId or not _values or not _values["_id"] then error("[MarcSync: Entry] Tried creating invalid Entry Object.") end
|
||||
local self = setmetatable({}, entry)
|
||||
self._tableId = _tableId
|
||||
self._values = _values
|
||||
self._objectId = _values["_id"]
|
||||
self._token = _token
|
||||
return self
|
||||
end
|
||||
return {
|
||||
new = function(tableId:string, entryData:typeof(types.EntryData), accessToken:string):typeof(Entry)
|
||||
if not tableId or not entryData or not entryData["_id"] or not accessToken then error("[MarcSync: Entry] Tried creating invalid Entry Object.") end
|
||||
local self = {}
|
||||
self._tableId = tableId
|
||||
self._entryData = entryData
|
||||
self._objectId = entryData["_id"]
|
||||
self._accessToken = accessToken
|
||||
|
||||
return entry
|
||||
self = setmetatable(self, {
|
||||
__index = Entry
|
||||
})
|
||||
|
||||
return self
|
||||
end
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user