Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudkit JS && Node JS

Tags:

I'm currently trying to perform server side connection to iCloud Server using the new CloudKit JS from Apple. According to the WWDC 2015 "CloudKit JS and Web Service", since CloudKit JS is a pure JS framework, you can use it in all JS environnements such as node JS.

I copied the source code of CloudKit JS from https://cdn.apple-cloudkit.com/ck/1/cloudkit.js and pasted it in a file named "cloudkit.js". Here is a demo of what I tried :

var CloudKit = require("/some/folders/cloudkit.js")  function demoPerformQuery() {   CloudKit.configure({       containers: [{     containerIdentifier: 'myContainerIdentifier',     apiToken: 'myAPIToken',     environment: 'development'       }]     })    var container = CloudKit.getDefaultContainer();   var publicDB = container.publicCloudDatabase;    publicDB.performQuery({recordType: 'Items'}).then(function(response){     // never called :-(   }) }  var express = require('express')  var app = express()  app.get("/", function(){   demoPerformQuery() })  var server = app.listen(8080, function () {   console.log("server launched") }) 

CloudKit seems to be correctly set up since all the functions are correctly called. But the callback of performQuery is never called. Why ?

Is there someone who already succeed to configure CloudKit JS in an server environnement ?

Thanks in advance

like image 958
GaétanZ Avatar asked Jun 20 '15 13:06

GaétanZ


People also ask

Is CloudKit a server?

CloudKit Now Supports Server-to-Server Web Service Requests In addition to providing a web interface for users to access the same data as your app, you can now easily read and write to the CloudKit public database from a server-side process or script with a server-to-server key.

What is CloudKit dashboard?

Overview. The CloudKit Database app is a web-based tool for developers to manage their iCloud containers. You can sign in to the CloudKit Database app through the Apple Developer web portal or through the CloudKit Console at https://icloud.developer.apple.com/.

Does CloudKit work in simulator?

CloudKit does not work with Simulator.


1 Answers

In the browser, CloudKit.js relies on XmlHttpRequest in order to fetch resources, but since CloudKit isn't an npm module you'll need a way to fetch things from your server.

npm install node-fetch

Using node-fetch, here is a tweaked version of your code that logs the resulting Items in your query:

var fetch = require('node-fetch'); var CloudKit = require("./cloudkit.js")  CloudKit.configure({   services: {     fetch: fetch   },   containers: [{     containerIdentifier: 'yourContainerIdentifier',     apiToken: 'yourAPItoken',     environment: 'development'   }] })  var container = CloudKit.getDefaultContainer(); var publicDB = container.publicCloudDatabase;  function demoPerformQuery() {   publicDB.performQuery({recordType: 'Items'}).then(function(response){     console.log(response)   }).catch(function(error){     console.log(error)   }) }  var express = require('express') var app = express()  app.get("/", function() {   demoPerformQuery() })  var server = app.listen(8080, function () {   console.log("Server listen") }) 

After hitting http://localhost:8080 you should see your server log the response to your query.

like image 115
Dave Browning Avatar answered Sep 19 '22 09:09

Dave Browning