forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request DefinitelyTyped#30265 from Deadly0/master
Types for meteor-astronomy.
- Loading branch information
Showing
4 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Type definitions for meteor-astronomy 2.6 | ||
// Project: https://github.com/jagi/meteor-astronomy/ | ||
// Definitions by: Igor Golovin <https://github.com/Deadly0> | ||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped | ||
// TypeScript Version: 2.8 | ||
|
||
/// <reference types="meteor" /> | ||
|
||
declare namespace MeteorAstronomy { | ||
type TypeOptionsPrimitives = typeof String | typeof Date | typeof Boolean | typeof Object | typeof Number; | ||
type TypeOptions = TypeOptionsPrimitives | TypeOptionsPrimitives[] | Class<any> | Enum<any>; | ||
type MongoQuery = object | string; | ||
|
||
interface SaveAndValidateOptions<K> { | ||
fields?: K[]; | ||
stopOnFirstError?: boolean; | ||
simulation?: boolean; | ||
cast?: boolean; | ||
} | ||
|
||
type SaveAndValidateCallback = (err: any, id: any) => void; | ||
type RemoveCallback = (err: any, result: any) => void; | ||
|
||
interface Validator { | ||
type: string; | ||
param: any; | ||
} | ||
|
||
interface ModelFullField<Field, Doc> { | ||
type: TypeOptions; | ||
optional?: boolean; | ||
transient?: boolean; | ||
immutable?: boolean; | ||
default?: () => Field | Field; | ||
index?: string | number; | ||
validators?: Validator[]; | ||
resolve?: (doc: Doc) => Field; | ||
} | ||
|
||
type ModelField<Field, Doc> = ModelFullField<Field, Doc> | TypeOptions; | ||
|
||
type Fields<T> = { | ||
[P in keyof T]: ModelField<T[P], T>; | ||
}; | ||
|
||
interface ClassModel<T> { | ||
name: string; | ||
collection?: Mongo.Collection<T>; | ||
fields: Fields<T>; | ||
behaviors?: object; | ||
secured?: { | ||
insert: boolean, | ||
update: boolean, | ||
remove: boolean, | ||
} | boolean; | ||
helpers?: object; | ||
events?: object; | ||
meteorMethods?: object; | ||
indexes?: object; | ||
} | ||
|
||
interface EnumModel<T> { | ||
name: string; | ||
identifiers: T[] | object; | ||
} | ||
|
||
type Model<T> = T & { | ||
set(fields: Partial<T>, options?: {cast?: boolean; clone?: boolean; merge?: boolean}): void; | ||
set(field: string, value: any): void; | ||
get(field: string): any; | ||
get(fields: string[]): any[]; | ||
isModified(field?: string): boolean; | ||
getModified(): any; | ||
getModifiedValues(options?: {old?: boolean, raw?: boolean}): Partial<T>; | ||
getModifier(): any; | ||
raw(): T; | ||
raw(field: string): any; | ||
raw(fields: string[]): any[]; | ||
save(options?: SaveAndValidateOptions<keyof T>, callback?: SaveAndValidateCallback): void; | ||
save(callback?: SaveAndValidateCallback): void; | ||
copy(save: boolean): any; | ||
validate(options?: SaveAndValidateOptions<keyof T>, callback?: SaveAndValidateCallback): void; | ||
validate(callback?: SaveAndValidateCallback): void; | ||
remove(callback?: RemoveCallback): void; | ||
}; | ||
|
||
interface Class<T> { | ||
new(data?: Partial<T>): Model<T>; | ||
|
||
findOne(query?: MongoQuery): Model<T>; | ||
find(query?: MongoQuery): Array<Model<T>>; | ||
update(search: object | string, query: object, callback?: () => void): void; | ||
} | ||
|
||
interface Enum<T> { | ||
getValues(): any[]; | ||
getIdentifier(identifier: T): any; | ||
} | ||
} | ||
|
||
declare module 'meteor/jagi:astronomy' { // tslint:disable-line:no-single-declare-module | ||
namespace Class { | ||
function create<T extends {}>(model: MeteorAstronomy.ClassModel<T>): MeteorAstronomy.Class<T>; | ||
} | ||
|
||
namespace Enum { | ||
function create<T>(model: MeteorAstronomy.EnumModel<T>): MeteorAstronomy.Enum<T>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { Class, Enum } from 'meteor/jagi:astronomy'; | ||
import { Meteor } from 'meteor/meteor'; | ||
import { Mongo } from 'meteor/mongo'; | ||
|
||
interface PostInterface { | ||
title: string; | ||
userId: string; | ||
publishedAt: Date; | ||
} | ||
|
||
const Posts = new Mongo.Collection<PostInterface>('posts'); | ||
|
||
const Post = Class.create<PostInterface>({ | ||
name: 'Post', | ||
collection: Posts, | ||
fields: { | ||
title: { | ||
type: String, | ||
validators: [{ | ||
type: 'minLength', | ||
param: 3 | ||
}] | ||
}, | ||
userId: String, | ||
publishedAt: Date | ||
}, | ||
behaviors: { | ||
timestamp: {} | ||
}, | ||
}); | ||
|
||
let post = new Post({ | ||
title: 'text', | ||
}); | ||
|
||
// Validate length of the "title" field. | ||
post.save(); | ||
|
||
// Notice that we call the "findOne" method | ||
// from the "Post" class not from the "Posts" collection. | ||
post = Post.findOne('id'); | ||
// Auto convert a string input value to a number. | ||
post.title = 'input[name=title]'; | ||
post.publishedAt = new Date(); | ||
// Check if all fields are valid and update document | ||
// with only the fields that have changed. | ||
post.save({fields: ['title']}); | ||
|
||
interface UserProfileInterface { | ||
nickname: string; | ||
firstName: string; | ||
createdAt: Date; | ||
age: number; | ||
} | ||
|
||
const UserProfile = Class.create<UserProfileInterface>({ | ||
name: 'UserProfile', | ||
fields: { | ||
nickname: String, | ||
firstName: String, | ||
createdAt: Date, | ||
age: Number, | ||
} | ||
}); | ||
|
||
interface UserInterface extends Meteor.User { | ||
address: object; | ||
phone: string; | ||
phoneNumber: string; | ||
} | ||
|
||
const User = Class.create<UserInterface>({ | ||
name: 'User', | ||
collection: Meteor.users as Mongo.Collection<UserInterface>, | ||
fields: { | ||
createdAt: Number, | ||
emails: { | ||
type: [Object], | ||
default: () => [], | ||
}, | ||
profile: { | ||
type: UserProfile, | ||
default: () => {}, | ||
}, | ||
address: { | ||
type: Object, | ||
optional: true | ||
}, | ||
phoneNumber: { | ||
type: String, | ||
}, | ||
phone: { | ||
type: String, | ||
resolve(doc) { | ||
return doc.phoneNumber; | ||
} | ||
} | ||
}, | ||
indexes: { | ||
fullName: { // Index name. | ||
fields: { // List of fields. | ||
phoneNumber: 1, | ||
createdAt: 1 | ||
}, | ||
options: {} | ||
} | ||
} | ||
}); | ||
|
||
const user = User.findOne(); | ||
user.set({username: 'user1'}); | ||
user.save(); | ||
|
||
enum IStatus { | ||
OPENED, CLOSED, DONE, CANCELED | ||
} | ||
|
||
const Status = Enum.create<IStatus>({ | ||
name: 'Status', | ||
identifiers: IStatus, | ||
}); | ||
|
||
const Issue = Class.create({ | ||
name: 'Issue', | ||
fields: { | ||
status: { | ||
type: Status | ||
} | ||
} | ||
}); | ||
|
||
Status.getValues(); // [0, 1, 2, 3] | ||
|
||
const StatusBis = Enum.create({ | ||
name: 'Status', | ||
identifiers: { | ||
OPENED: 5, | ||
CLOSED: null, | ||
DONE: 15, | ||
CANCELED: undefined | ||
} | ||
}); | ||
|
||
StatusBis.getValues(); // [5, 6, 15, 16] | ||
|
||
const statusNumber = IStatus.OPENED; | ||
|
||
Status.getIdentifier(statusNumber); // "OPENED" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"lib": [ | ||
"es6", | ||
"dom" | ||
], | ||
"noImplicitAny": true, | ||
"noImplicitThis": true, | ||
"strictNullChecks": false, | ||
"strictFunctionTypes": true, | ||
"baseUrl": "../", | ||
"typeRoots": [ | ||
"../" | ||
], | ||
"types": [], | ||
"noEmit": true, | ||
"forceConsistentCasingInFileNames": true | ||
}, | ||
"files": [ | ||
"index.d.ts", | ||
"meteor-astronomy-tests.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "dtslint/dt.json" | ||
} |