Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(typings): 改善和纠正各种 id 类型的性质 #481

Merged
merged 1 commit into from
Jun 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/apis/event/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,5 @@ export function timeToDate(date: string, returnValue?: boolean) {
* 使用该函数根据实例上的 _id 获得原 _id。
*/
export const originEventId = (id: EventId): EventId => {
return id.split('_', 1)[0]
return id.split('_', 1)[0] as EventId
}
2 changes: 1 addition & 1 deletion src/schemas/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface EventSchema {
endDate: string
untilDate: string
isAllDay: boolean
involveMembers: string []
involveMembers: UserId[]
_projectId: ProjectId
_scenariofieldconfigId?: ScenarioFieldConfigId
_sourceId: EventId
Expand Down
12 changes: 8 additions & 4 deletions src/schemas/Member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export interface MemberProfileSchema {
teamIds: TeamId[]
}

export interface MemberSchema {
export interface GeneralMemberSchema {
_boundToObjectId: ProjectId | OrganizationId
_id: String // 兼容新(MemberId)和旧(UserId),当完成迁移,换为更准确的 MemberId
_id: string // 兼容新(MemberId)和旧(UserId),当完成迁移,换为更准确的 MemberId
_memberId: MemberId
_roleId: RoleId
_userId: UserId
Expand Down Expand Up @@ -52,11 +52,15 @@ export interface MemberSchema {
website: string
}

export interface LegacyMemberSchema extends MemberSchema {
export interface MemberSchema extends GeneralMemberSchema {
_id: MemberId
}

export interface LegacyMemberSchema extends GeneralMemberSchema {
_id: UserId
}

const Schema: SchemaDef<MemberSchema> = {
const Schema: SchemaDef<GeneralMemberSchema> = {
_boundToObjectId: { type: RDBType.STRING },
_id: { type: RDBType.STRING, primaryKey: true },
_memberId: { type: RDBType.STRING },
Expand Down
4 changes: 1 addition & 3 deletions src/schemas/TapDashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ export interface TapCoordination {
size?: TapCoordSize
}

export interface TapDashboardSectionId extends String {
kind?: 'TapDashboardSectionId'
}
export type TapDashboardSectionId = string & { kind: 'TapDashboardSectionId' }

export interface TapDashboardSection {
_id: TapDashboardSectionId
Expand Down
4 changes: 1 addition & 3 deletions src/schemas/TapQuestion.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { TapChartType } from './TapChart'
import { TapGenericFilterResponse } from 'teambition-types'

export interface TapQuestionId extends String {
kind?: 'QuestionId'
}
export type TapQuestionId = string & { kind: 'TapQuestionId' }

export interface TapQuestion {
_id: TapQuestionId
Expand Down
8 changes: 2 additions & 6 deletions src/schemas/UserMe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ export interface UserPaymentPlan {
status: string
}

export interface StrikerToken extends String {
kind?: 'StrikerToken'
}
export type StrikerToken = string & { kind: 'StrikerToken' }

export interface TcmToken extends String {
kind?: 'TcmToken'
}
export type TcmToken = string & { kind: 'TcmToken' }

export interface UserMe {
_id: UserId
Expand Down
96 changes: 48 additions & 48 deletions src/teambition.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
// id

declare module 'teambition-types' {
export interface ActivenessId extends String { kind?: 'ActivenessId' }
export interface ActivityId extends String { kind?: 'ActivityId' }
export interface AdvancedCustomFieldId extends String { kind?: 'AdvancedCustomFieldId' }
export interface ApplicationId extends String { kind?: 'ApplicationId' }
export interface CollectionId extends String { kind?: 'CollectionId' }
export interface CustomFieldCategoryId extends String { kind?: 'CustomFieldCategoryId' }
export interface CustomFieldChoiceId extends String { kind?: 'CustomFieldChoiceId' }
export interface CustomFieldId extends String { kind?: 'CustomFieldId' }
export interface CustomFieldLinkId extends String { kind?: 'CustomFieldLinkId' }
export interface CustomFieldValueId extends String { kind?: 'CustomFieldValueId' }
export interface CustomRoleId extends String { kind?: 'CustomRoleId' }
export interface DashboardCardId extends String { kind?: 'DashboardCardId' }
export interface EntryCategoryId extends String { kind?: 'EntryCategoryId' }
export interface EntryId extends String { kind?: 'EntryId' }
export interface EventId extends String { kind?: 'EventId' }
export interface FeedbackId extends String { kind?: 'FeedbackId' }
export interface FileId extends String { kind?: 'FileId' }
export interface GroupId extends String { kind?: 'GroupId' }
export interface HomeActivityId extends String { kind?: 'HomeActivityId' }
export interface MemberId extends String { kind?: 'MemberId' }
export interface MessageId extends String { kind?: 'MessageId' }
export interface ObjectLinkId extends String { kind?: 'ObjectLinkId' }
export interface OrganizationId extends String { kind?: 'OrganizationId' }
export interface PostId extends String { kind?: 'PostId' }
export interface PreferenceId extends String { kind?: 'PreferenceId' }
export interface ProjectBoardId extends String { kind?: 'ProjectBoardId' }
export interface ProjectId extends String { kind?: 'ProjectId' }
export interface ProjectTagId extends String { kind?: 'ProjectTagId' }
export interface RoomId extends String { kind?: 'RoomId' }
export interface ScenarioFieldId extends String { kind?: 'ScenarioFieldId' }
export interface ScenarioFieldConfigId extends String { kind?: 'ScenarioFieldConfigId' }
export interface SmartGroupId extends String { kind?: 'SmartGroupId' }
export interface SprintId extends String { kind?: 'SprintId' }
export interface StageId extends String { kind?: 'StageId' }
export interface SubscribeId extends String { kind?: 'SubscribeId' }
export interface SubtaskId extends String { kind?: 'SubtaskId' }
export interface TagCategoryId extends String { kind?: 'TagCategoryId' }
export interface TagId extends String { kind?: 'TagId' }
export interface TapChartId extends String { kind?: 'TapChartId' }
export interface TapDashboardId extends String { kind?: 'TapDashboardId' }
export interface TaskflowId extends String { kind?: 'TaskflowId' }
export interface TaskflowStatusId extends String { kind?: 'TaskflowStatusId' }
export interface TaskId extends String { kind?: 'TaskId' }
export interface TasklistId extends String { kind?: 'TasklistId' }
export interface TeamId extends String { kind?: 'TeamId' }
export interface UserId extends String { kind?: 'UserId' }
export interface VersionId extends String { kind?: 'VersionId' }
export interface WorkId extends String { kind?: 'WorkId' }
export type ActivenessId = string & { kind: 'ActivenessId' }
export type ActivityId = string & { kind: 'ActivityId' }
export type AdvancedCustomFieldId = string & { kind: 'AdvancedCustomFieldId' }
export type ApplicationId = string & { kind: 'ApplicationId' }
export type CollectionId = string & { kind: 'CollectionId' }
export type CustomFieldCategoryId = string & { kind: 'CustomFieldCategoryId' }
export type CustomFieldChoiceId = string & { kind: 'CustomFieldChoiceId' }
export type CustomFieldId = string & { kind: 'CustomFieldId' }
export type CustomFieldLinkId = string & { kind: 'CustomFieldLinkId' }
export type CustomFieldValueId = string & { kind: 'CustomFieldValueId' }
export type CustomRoleId = string & { kind: 'CustomRoleId' }
export type DashboardCardId = string & { kind: 'DashboardCardId' }
export type EntryCategoryId = string & { kind: 'EntryCategoryId' }
export type EntryId = string & { kind: 'EntryId' }
export type EventId = string & { kind: 'EventId' }
export type FeedbackId = string & { kind: 'FeedbackId' }
export type FileId = string & { kind: 'FileId' }
export type GroupId = string & { kind: 'GroupId' }
export type HomeActivityId = string & { kind: 'HomeActivityId' }
export type MemberId = string & { kind: 'MemberId' }
export type MessageId = string & { kind: 'MessageId' }
export type ObjectLinkId = string & { kind: 'ObjectLinkId' }
export type OrganizationId = string & { kind: 'OrganizationId' }
export type PostId = string & { kind: 'PostId' }
export type PreferenceId = string & { kind: 'PreferenceId' }
export type ProjectBoardId = string & { kind: 'ProjectBoardId' }
export type ProjectId = string & { kind: 'ProjectId' }
export type ProjectTagId = string & { kind: 'ProjectTagId' }
export type RoomId = string & { kind: 'RoomId' }
export type ScenarioFieldId = string & { kind: 'ScenarioFieldId' }
export type ScenarioFieldConfigId = string & { kind: 'ScenarioFieldConfigId' }
export type SmartGroupId = string & { kind: 'SmartGroupId' }
export type SprintId = string & { kind: 'SprintId' }
export type StageId = string & { kind: 'StageId' }
export type SubscribeId = string & { kind: 'SubscribeId' }
export type SubtaskId = string & { kind: 'SubtaskId' }
export type TagCategoryId = string & { kind: 'TagCategoryId' }
export type TagId = string & { kind: 'TagId' }
export type TapChartId = string & { kind: 'TapChartId' }
export type TapDashboardId = string & { kind: 'TapDashboardId' }
export type TaskflowId = string & { kind: 'TaskflowId' }
export type TaskflowStatusId = string & { kind: 'TaskflowStatusId' }
export type TaskId = string & { kind: 'TaskId' }
export type TasklistId = string & { kind: 'TasklistId' }
export type TeamId = string & { kind: 'TeamId' }
export type UserId = string & { kind: 'UserId' }
export type VersionId = string & { kind: 'VersionId' }
export type WorkId = string & { kind: 'WorkId' }
}

// computed id
Expand Down
5 changes: 3 additions & 2 deletions test/apis/customfieldlink.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { expect } from 'chai'
import { SDKFetch, createSdk, SDK } from '../'
import { customFieldLink } from '../fixtures/customfieldlinks.fixture'
import { mock, expectToDeepEqualForFieldsOfTheExpected } from '../utils'
import { ProjectId } from 'teambition-types'

const fetchMock = require('fetch-mock')

Expand All @@ -29,7 +30,7 @@ describe('CustomFieldLinkApi request spec: ', () => {
})

it('should return a CustomFieldLink array', function* () {
const projectId = customFieldLink._projectId
const projectId = customFieldLink._projectId as ProjectId
const customFieldLinks = [customFieldLink]
const url = `/projects/${projectId}/customfieldlinks?boundType=application&_=666`

Expand All @@ -51,7 +52,7 @@ describe('CustomFieldLinkApi spec: ', () => {
})

it('should return a CustomFieldLink array', function* () {
const projectId = customFieldLink._projectId
const projectId = customFieldLink._projectId as ProjectId
const customFieldLinks = [customFieldLink]
mockResponse(customFieldLinks)

Expand Down
13 changes: 7 additions & 6 deletions test/apis/event/EventGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../../fixtures/events.fixture'
import { EventGenerator } from '../../../src/apis/event/EventGenerator'
import { clone } from '../../index'
import { EventId } from 'teambition-types'

describe('EventGenerator spec', () => {
let eventGenerator: EventGenerator
Expand Down Expand Up @@ -297,31 +298,31 @@ describe('EventGenerator spec', () => {

it('findByEventId() should work on a normal event', () => {
const _eventGenerator = new EventGenerator(normalEvent as any)
const targetId = normalEvent._id
const targetId = normalEvent._id as EventId
const invalidId = normalEvent._id + 'asdf'

expect(_eventGenerator.findByEventId(targetId)).to.deep.equal(normalEvent)
expect(_eventGenerator.findByEventId(invalidId)).to.be.null
expect(_eventGenerator.findByEventId(invalidId as EventId)).to.be.null
})

it('findByEventId() should return null for a recurrent event with an un-timestamped id', () => {
const targetId = recurrenceByMonth._id
const targetId = recurrenceByMonth._id as EventId

expect(eventGenerator.findByEventId(targetId)).to.be.null
})

it('findByEventId() should work on a recurrent event', () => {
let timestamp = new Date(recurrenceByMonth.startDate).valueOf()
let targetId = recurrenceByMonth._id + '_' + timestamp
let targetId = recurrenceByMonth._id + '_' + timestamp as EventId
expect(eventGenerator.findByEventId(targetId)).to.deep.equal(eventGenerator.next().value)

timestamp = Moment(recurrenceByMonth.startDate).add(2, 'months').valueOf()
targetId = recurrenceByMonth._id + '_' + timestamp
targetId = (recurrenceByMonth._id + '_' + timestamp) as EventId
eventGenerator.next()
expect(eventGenerator.findByEventId(targetId)).to.deep.equal(eventGenerator.next().value)

const timestampExDate = new Date(recurrenceStartAtAnExcludedDate.startDate).valueOf()
const targetIdExDate = recurrenceStartAtAnExcludedDate._id + '_' + timestampExDate
const targetIdExDate = recurrenceStartAtAnExcludedDate._id + '_' + timestampExDate as EventId
const eventGeneratorExDate = new EventGenerator(recurrenceStartAtAnExcludedDate as any)
expect(eventGeneratorExDate.findByEventId(targetIdExDate)).to.be.null
})
Expand Down
11 changes: 6 additions & 5 deletions test/apis/event/event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { expect } from 'chai'
import { createSdk, SDK, SocketMock, EventSchema } from '../../index'
import * as Fixture from '../../fixtures/events.fixture'
import { mock, restore, equals, looseDeepEqual, clone } from '../../utils'
import { EventId } from 'teambition-types'

describe('EventApi request spec', () => {
let sdk: SDK
Expand All @@ -22,7 +23,7 @@ describe('EventApi request spec', () => {
const fixture = Fixture.normalEvent
mockResponse(fixture)

yield sdk.getEvent(fixture._id)
yield sdk.getEvent(fixture._id as EventId)
.values()
.do(([r]) => {
const result = r.next().value
Expand All @@ -34,7 +35,7 @@ describe('EventApi request spec', () => {
const fixture = Fixture.recurrenceByMonth
mockResponse(fixture)

yield sdk.getEvent(fixture._id)
yield sdk.getEvent(fixture._id as EventId)
.values()
.do(([r]) => {
const result = r.next().value
Expand All @@ -58,7 +59,7 @@ describe('EventApi request spec', () => {
const fixture = Fixture.recurrenceByMonth
mockResponse(fixture)

const signal = sdk.getEvent(fixture._id)
const signal = sdk.getEvent(fixture._id as EventId)
.changes()

signal.subscribe()
Expand All @@ -83,13 +84,13 @@ describe('EventApi request spec', () => {
const fixture = Fixture.recurrenceByMonth
mockResponse(fixture)

const token1 = sdk.getEvent(fixture._id)
const token1 = sdk.getEvent(fixture._id as EventId)

const f2 = clone(fixture)
f2._id = 'mockF2Id'
mockResponse(f2)

const token2 = sdk.getEvent(f2._id)
const token2 = sdk.getEvent(f2._id as EventId)

yield token1.combine(token2)
.values()
Expand Down
3 changes: 2 additions & 1 deletion test/apis/file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { expect } from 'chai'
import { createSdk, SDK, SocketMock, FileSchema } from '../index'
import * as Fixture from '../fixtures/files.fixture'
import { mock, restore, looseDeepEqual, expectToDeepEqualForFieldsOfTheExpected } from '../utils'
import { FileId } from 'teambition-types'

describe('FileApi request spec', () => {
let sdk: SDK
Expand All @@ -21,7 +22,7 @@ describe('FileApi request spec', () => {
const [ fixture ] = Fixture.projectFiles
mockResponse(fixture)

yield sdk.getFile(fixture._id)
yield sdk.getFile(fixture._id as FileId)
.values()
.do(([r]) => {
expectToDeepEqualForFieldsOfTheExpected(r, fixture)
Expand Down
15 changes: 9 additions & 6 deletions test/apis/like.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { describe, it, beforeEach, afterEach } from 'tman'
import { createSdk, SDK, LikeSchema } from '../index'
import like from '../fixtures/like.fixture'
import { mock, restore } from '../utils'
import { DetailObjectId } from 'teambition-types'

describe('LikeApi request spec: ', () => {
let sdk: SDK
let mockResponse: <T>(m: T, delay?: number | Promise<any>) => void
const mockTaskId = 'mocktask' as DetailObjectId
const mockTaskLikeId = 'mocktask:like'

beforeEach(() => {
sdk = createSdk()
Expand All @@ -21,7 +24,7 @@ describe('LikeApi request spec: ', () => {
it('get like should pass', function* () {
mockResponse(like)

yield sdk.getLike('task', 'mocktask')
yield sdk.getLike('task', mockTaskId)
.values()
.do(([r]) => {
delete r._id
Expand All @@ -32,15 +35,15 @@ describe('LikeApi request spec: ', () => {
it('toggle like should pass', function* () {
yield sdk.database.insert('Like', {
...like,
_id: 'mocktask:like'
_id: mockTaskLikeId
})

mockResponse({ ...like, isLike: false })

yield sdk.toggleLike('task', 'mocktask', true)
yield sdk.toggleLike('task', mockTaskId, true)

yield sdk.database.get<LikeSchema>('Like', {
where: { _id: 'mocktask:like' }
where: { _id: mockTaskLikeId }
})
.values()
.do(([r]) => {
Expand All @@ -49,10 +52,10 @@ describe('LikeApi request spec: ', () => {

mockResponse({ ...like, isLike: true })

yield sdk.toggleLike('task', 'mocktask', false)
yield sdk.toggleLike('task', mockTaskId, false)

yield sdk.database.get<LikeSchema>('Like', {
where: { _id: 'mocktask:like' }
where: { _id: mockTaskLikeId }
})
.values()
.do(([r]) => {
Expand Down
3 changes: 2 additions & 1 deletion test/apis/my.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { createSdk, SDK, TaskSchema } from '../index'
import { EventGenerator } from '../../src/apis/event/EventGenerator'
import * as Fixture from '../fixtures/my.fixture'
import { mock, restore, expectToDeepEqualForFieldsOfTheExpected } from '../utils'
import { UserId } from 'teambition-types'

describe('MyApi request spec', () => {
const userId = Fixture.myRecent[0]['_executorId']
const userId = Fixture.myRecent[0]['_executorId'] as UserId
let sdk: SDK
let mockResponse: <T>(m: T, delay?: number | Promise<any>) => void

Expand Down
Loading