From 7dddb84c8ea473da1bb0aa61db9ac97b0cbcdea9 Mon Sep 17 00:00:00 2001 From: Sergey Shliakhov Date: Tue, 25 Feb 2020 13:23:13 +0100 Subject: [PATCH 1/2] Add create project method --- src/dav/client.ts | 51 +++++++++++++++++++++++----------- src/dav/project.ts | 8 ++++++ src/examples/create-project.ts | 31 +++++++++++++++++++++ 3 files changed, 74 insertions(+), 16 deletions(-) create mode 100644 src/dav/project.ts create mode 100644 src/examples/create-project.ts diff --git a/src/dav/client.ts b/src/dav/client.ts index 68bbfa7..2fcfb84 100644 --- a/src/dav/client.ts +++ b/src/dav/client.ts @@ -6,6 +6,7 @@ import axios, { import { FileProps } from './fileProps' import { Tag } from './tag' import { MultiStatusResponse } from './multiStatusResponse' +import { Project } from './project' export class Client { constructor(readonly connection: AxiosInstance) {} @@ -121,6 +122,40 @@ export class Client { } } + createTag = async (name: string): Promise => { + const response = await this.connection({ + method: 'POST', + url: '/systemtags', + data: { + userVisible: true, + userAssignable: true, + canAssign: true, + name, + }, + }) + const url = response.headers['content-location'] + const id = this._parseIdFromLocation(url) + return new Tag(id, name) + } + + createProject = async ( + username: string, + name: string, + foreignId: string, + ): Promise => { + const response = await this.connection({ + method: 'POST', + url: `/projects/${username}`, + data: { + name, + 'foreign-id': foreignId, + }, + }) + const data = response.data + const url = response.headers['content-location'] + return new Project(data.id, data.name, foreignId, url) + } + private _props = async ( path: string, names: string[], @@ -145,22 +180,6 @@ export class Client { return this._parseMultiStatus(rawResponse.data) } - createTag = async (name: string): Promise => { - const response = await this.connection({ - method: 'POST', - url: '/systemtags', - data: { - userVisible: true, - userAssignable: true, - canAssign: true, - name, - }, - }) - const url = response.headers['content-location'] - const id = this._parseIdFromLocation(url) - return new Tag(id, name) - } - private _parseIdFromLocation = (url: string): string => { const queryPos = url.indexOf('?') if (queryPos > 0) { diff --git a/src/dav/project.ts b/src/dav/project.ts new file mode 100644 index 0000000..d7af803 --- /dev/null +++ b/src/dav/project.ts @@ -0,0 +1,8 @@ +export class Project { + constructor( + readonly id: string, + readonly name: string, + readonly foreignID: string, + readonly url: string, + ) {} +} diff --git a/src/examples/create-project.ts b/src/examples/create-project.ts new file mode 100644 index 0000000..13b76dd --- /dev/null +++ b/src/examples/create-project.ts @@ -0,0 +1,31 @@ +import { Client, FileProps, Tag } from '../index' +import { AxiosRequestConfig } from 'axios' +import { Project } from 'src/dav/project' + +const username = 'username' +const token = + '1gtlgCsANyXdTUFLgmWZINwKnqHPZYEZZQNr4ARUouONSjrFfdVGqWB1AEuuz9jtOcq3u7fD' + +const projectname = 'project-1' +const baseURL = 'http://localhost/remote.php/dav/' + +const config: AxiosRequestConfig = { + baseURL, + headers: { Authorization: `Bearer ${token}` }, +} + +const run = async () => { + try { + const dav: Client = Client.create(config) + const project: Project = await dav.createProject( + username, + projectname, + 'foreign-id', + ) + console.log(project.id) + } catch (error) { + console.log(error) + } +} + +run().then() From 0aa2ae325029beaf21b4823bccd47c63b2326da3 Mon Sep 17 00:00:00 2001 From: Sergey Shliakhov Date: Tue, 25 Feb 2020 15:23:06 +0100 Subject: [PATCH 2/2] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 25e0d9b..8b782be 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nextcloud-webdav-client", - "version": "1.0.0", + "version": "1.1.0", "description": "Wrapper for nextcloud webdav", "main": "build/src/index.js", "types": "build/src/index.d.ts",