Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniel Lin committed Nov 23, 2020
0 parents commit a36308a
Show file tree
Hide file tree
Showing 13 changed files with 296 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
106 changes: 106 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
built

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package-lock.json
node_modules/
doc/
built/
7 changes: 7 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"singleQuote": true,
"semi": true,
"printWidth": 100,
"tabWidth": 2,
"useTabs": false
}
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "ts-dav",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"private": false,
"dependencies": {
"cross-fetch": "^3.0.6",
"xml-js": "^1.6.11"
},
"devDependencies": {
"prettier": "^2.2.0",
"typescript": "^4.1.2"
}
}
5 changes: 5 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class DAVClient {
url: string;

async Login(credential: Credential) {}
}
17 changes: 17 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const enum DAVNamespace {
CALENDAR_SERVER = 'http://calendarserver.org/ns/',
CALDAV_APPLE = 'http://apple.com/ns/ical/',
CALDAV = 'urn:ietf:params:xml:ns:caldav',
CARDDAV = 'urn:ietf:params:xml:ns:carddav',
DAV = 'DAV:',
}

export const enum DAVMethod {
COPY = 'COPY',
LOCK = 'LOCK',
MKCOL = 'MKCOL',
MOVE = 'MOVE',
PROPFIND = 'PROPFIND',
PROPPATCH = 'PROPPATCH',
UNLOCK = 'UNLOCK',
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
1 change: 1 addition & 0 deletions src/model/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
66 changes: 66 additions & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type { DAVNamespace, DAVMethod } from './consts';
import convert from 'xml-js';
import { fetch } from 'cross-fetch';
import { nativeType } from './util/nativeType';

export async function davRequest(
url: string,
options: {
headers: any;
method: DAVMethod;
body: any;
namespace: DAVNamespace;
attributes: { [key: string]: string };
}
) {
const { headers, body, namespace, method, attributes } = options;
const xmlBody = convert.js2xml(body, {
compact: true,
spaces: 2,
elementNameFn: (name) => {
// add namespace to all keys without namespace
if (namespace && !/^.+:.+/.test(name)) {
return `${namespace}:${name}`;
}
return name;
},
});

const davResponse = await fetch(url, {
headers: { 'Content-Type': 'text/xml;charset=UTF-8', ...headers },
body: xmlBody,
method,
});

const result = convert.xml2js(await davResponse.text(), {
compact: true,
trim: true,
textFn: (value: any, parentElement: any) => {
try {
// This is needed for xml-js design reasons
// eslint-disable-next-line no-underscore-dangle
const parentOfParent = parentElement._parent;
const pOpKeys = Object.keys(parentOfParent);
const keyNo = pOpKeys.length;
const keyName = pOpKeys[keyNo - 1];
const arrOfKey = parentOfParent[keyName];
const arrOfKeyLen = arrOfKey.length;
if (arrOfKeyLen > 0) {
const arr = arrOfKey;
const arrIndex = arrOfKey.length - 1;
arr[arrIndex] = nativeType(value);
} else {
parentOfParent[keyName] = nativeType(value);
}
} catch (e) {
console.error(e);
}
},
// remove namespace
elementNameFn: (attributeName) => attributeName.replace(/^.+:/, ''),
ignoreAttributes: true,
ignoreDeclaration: true,
});

return result;
}
14 changes: 14 additions & 0 deletions src/util/nativeType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const nativeType = (value: any) => {
const nValue = Number(value);
if (!Number.isNaN(nValue)) {
return nValue;
}
const bValue = value.toLowerCase();
if (bValue === 'true') {
return true;
}
if (bValue === 'false') {
return false;
}
return value;
};
21 changes: 21 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"sourceMap": true,
"esModuleInterop": true,
"allowJs": false,
"noImplicitAny": true,
"resolveJsonModule": true,
"moduleResolution": "node",
"lib": ["es2018", "dom"],
"module": "commonjs",
"target": "es2018",
"baseUrl": ".",
"paths": {
"*": ["node_modules/*", "src/types/*"]
},
"typeRoots": ["node_modules/@types", "src/types"],
"outDir": "./built"
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
37 changes: 37 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


cross-fetch@^3.0.6:
version "3.0.6"
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.0.6.tgz#3a4040bc8941e653e0e9cf17f29ebcd177d3365c"
integrity sha512-KBPUbqgFjzWlVcURG+Svp9TlhA5uliYtiNx/0r8nv0pdypeQCRJ9IaSIc3q/x3q8t3F75cHuwxVql1HFGHCNJQ==
dependencies:
node-fetch "2.6.1"

[email protected]:
version "2.6.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==

prettier@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.2.0.tgz#8a03c7777883b29b37fb2c4348c66a78e980418b"
integrity sha512-yYerpkvseM4iKD/BXLYUkQV5aKt4tQPqaGW6EsZjzyu0r7sVZZNPJW4Y8MyKmicp6t42XUPcBVA+H6sB3gqndw==

sax@^1.2.4:
version "1.2.4"
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==

typescript@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.2.tgz#6369ef22516fe5e10304aae5a5c4862db55380e9"
integrity sha512-thGloWsGH3SOxv1SoY7QojKi0tc+8FnOmiarEGMbd/lar7QOEd3hvlx3Fp5y6FlDUGl9L+pd4n2e+oToGMmhRQ==

xml-js@^1.6.11:
version "1.6.11"
resolved "https://registry.yarnpkg.com/xml-js/-/xml-js-1.6.11.tgz#927d2f6947f7f1c19a316dd8eea3614e8b18f8e9"
integrity sha512-7rVi2KMfwfWFl+GpPg6m80IVMWXLRjO+PxTq7V2CDhoGak0wzYzFgUY2m4XJ47OGdXd8eLE8EmwfAmdjw7lC1g==
dependencies:
sax "^1.2.4"

0 comments on commit a36308a

Please sign in to comment.